Imagick or header issue in wordpress and save that image to specific directory

Hi i write some code for images to be put side by side.I use Imagick library for that purpose.This is my code.

$im = new Imagick();
// session contain image path like upload/my.jpg
$im->readImage("http://localhost/wordpress3.5/".$_SESSION['imgname']);
$im->readImage("http://localhost/wordpress3.5/".$_SESSION['preimgurl']);

$im->resetIterator();
$combined = $im->appendImages(false);

/* Output the image */
$combined->setImageFormat("png");
header("Content-Type: image/png");
echo $combined;exit();

But the output is not what i suppose to be.this is output.I write this code under the plugin/ files.Also i want to save that image to directory like "localhost/wordpress/uplaod_pic/". enter image description here

Solutions

You should use an image tag with the src tags containing the direct or base64 version of the image.

First example would be direct:

echo '<img src="http://localhost/wordpress3.5/'.$_SESSION['imgname'].'">';

Second would be using base64 with the data URI scheme, PHP Example:

echo '<img src="data:image/png;base64,'.base64_encode($combined).'">';

I would recommend the first method, simply because it looks like a product display, the images are most likely public and hotlinking can be busted if you choose.

In both examples, since the image is inline, you wouldn't need to set the content-type to an image.

You should also place the correct mime type within the second version, either image/png , image/jpeg or others depending on the image.

Edit : To extend this:

$combined contains the image, so you want to save this, simple use:

file_put_contents('upload_pic/'.$time().'.png', $combined);

Similar questions

PHP upload issue with WordPress -> Want to upload to theme directory, but function is local to admin directory
I am trying to write a upload function in the WP functions.php. It works great, uploading the file to wp-admin, I want it to go into the themes directory - which I can access with get_bloginfo... but that is an absoluter address. Is there a way to upload a file to a directory that is in the same site, but not the same directory? The file structure ...
How can add metabox for post of specific category before save post and after save post
I want to add Metabox for the posts of category=5 and i am using the following code but unable to do so . So please help me out->
how to keep theme background image, menu or site header image while moving site in sub directory?
I'm moving site root to sub directory, menu background image or site header display as default, I have change URL in db as set all sub directory location also set .htaccess but my custom background or header and menu not display as before? I'm used twenty ten theme.
Save the output image into a different directory, with a new filename and extension
Let say I have this code in my "image.php" file that will create a new GD image stream and output an image: Here's my sample diagram: file -> /root/template/program/image.php directory -> /root/template/images How can I save the outputted image of "image.php" into the "/images" directory, with a different filename and extension like "file.png"?
Random header image but also specific header for certain pages
I have a question I'm using a theme where the header image changes in different pages of the page because is set like that. But the main header image stays in all the remain pages.. What I want to do is to randomize 2 main header images everytime you refresh the page but leave the other header images to the other pages... I don't know if I explain ...
Restrict IIS virtual directory to specific HTTP host header?
I have a website setup in IIS with 2 HTTP Headers (www.mysite.com and [blank]). There is a virtual directory called 'blog' which points to a WordPress site - this is accessed via www.mysite.com/blog/ Any other requests (e.g. www.site1.com, www.site2.com, something.mysite.com) are handled by the same site (using the [blank] header) and rewrite rules...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .