Create image from url: multiple problems - php

<?php
if(isset($_GET['img']) && is_numeric($_GET['img'])){
$img = $_GET['img'];
$imgarray = array (
'1' => 'http://www.path/to/image1.png',
'2' => 'http://www.path/to/image2.png',
'3' => 'http://www.path/to/image3.png'
);
$src = $imgarray[$img];
header('Content-type: image/png');
echo file_get_contents($src);
}
else
{
header('Content-type: image/png');
echo 'Image could not be loaded';
}
?>
Hello again stackoverflow!
Im having multiple problems.
1: When the $_GET['img'] is set and its numeric, the image will be displayed right, but i want to add text in the upper-right corner of the image... How can i do that? I've looked through multiple GD tutorials and examples but i can't find my answer.
2: When $_GET['img'] isn't set i want to display the text: Image could not be loaded. How cna i do that? Because this doesn't seem to work...
Greetings

What you'll have to do is use GD. Load up the requested image into PHP with imagecreatefrompng(), since you have listed pngs in your array, you'd have to use imagecreatefromjpeg() or whatever depending on their format. Then use one of the text writers like imagestring() to write the text to the location in the image resource returned by imagecreatefrompng(), then return the image resource to the browser.
Can also use one of the functions that uses an external font, like imagettftext(), but would need to have the appropriate font to use on the server.
For the error, if you want it to be an image, you'll need to use imagecreatetruecolor() to make a new image, then use imagecolorallocate() to assign a color palette to it, then use imagestring() to write the error message to the image and return it. Of course, probably be easier just to make an error image in GIMP or something and return it, rather than going through the trouble of generating a new error image each time.

Just remove the line that says header('Content-type: image/png'); in your else{} block
That will do the trick. At the moment your are telling the user's browser to treat that text as an image, of course that can't work. If you want an image with the text "Image could not be loaded", it's more complicated than that...

Related

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/".
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);

php iphone/IOS6 upload rotation issue: what is best way to save rotated image

Using the safari mobile browser with IOS6, the file upload function gives users the option to snap a photo. Unfortunately, upon snapping the photo, while the photo thumb shows up properly in the browser, when you upload to a server, the file is rotated 90 degrees. This appears to be due to the exif data that the iphone sets. I have code that fixes the orientation by rotating the image when serving. However, I suspect it would be better to save the rotated, properly oriented, image so I no longer have to worry about orientation. Many of my other photos do not even have exif data and i don't want to mess with it if I can avoid it.
Can anyone suggest code to save the image so it is properly oriented?
Here is the code that rotates the image. The following code will display the properly oriented image, however, what I want to do is save it so I can then serve it whenever I want without worrying about orientation.
Also I would like to replace impagejpeg call in code below so that any code works for gifs as well as jpgs.
Thanks for suggestions/code!
PHP
//Here is sample image after uploaded to server and moved to a directory
$target = "pics/779_pic.jpg";
$source = imagecreatefromstring(file_get_contents($target));
$exif = exif_read_data($target);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($source,90,0);
//echo 'It is 8';
break;
case 3:
$image = imagerotate($source,180,0);
//echo 'It is 3';
break;
case 6:
$image = imagerotate($source,-90,0);
//echo 'It is 6';
break;
}
}
// $image now contains a resource with the image oriented correctly
//This is where I want to save resource properly oriented instead of display.
header('Content-type: image/jpg');
imagejpeg($image);
?>
Only JPEG or TIFF files can carry EXIF metadata, so there's no need to worry about handling GIFs (or PNGs, for that matter) with your code.
From page 9 of what I believe is the official specification:
Compressed files are recorded as JPEG (ISO/IEC 10918-1) with application marker segments (APP1 and APP2) inserted. Uncompressed files are recorded in TIFF Rev. 6.0 format.
http://www.cipa.jp/english/hyoujunka/kikaku/pdf/DC-008-2010_E.pdf
To save your image just use the same function imagejpeg and the next parameter to save the image, something like:
imagejpeg($image, $target, 100);
In this case you don't need the specify the header, because you are not showing nothing.
Reference:
http://sg3.php.net/manual/en/function.imagejpeg.php

Adding of Image Layer Fails (GD) PHP

I've installed the GD Library on my Apache just now, and it seems that my script below doesn't work.
I'm trying to add a layer "play.png" to a youtube video thumbnail (http://img.youtube.com/vi/VIDEOID/default.jpg)
I've tried it with many different videoID's but the image doesn't load. There is a message that the graphic couldn't be opened because it contains errors.
I'm opening the file with postimage.php?v=7yV_JtFnIwo
http://img.youtube.com/vi/7yV_JtFnIwo/default.jpg opens correctly too...
Does anyone know where the issue could be?
Thanks in advance!
<?php
// The header line informs the server of what to send the output
// as. In this case, the server will see the output as a .png
// image and send it as such
header ("Content-type: image/png");
// Defining the background image. Optionally, a .jpg image could
// could be used using imagecreatefromjpeg, but I personally
// prefer working with png
$background = imagecreatefromjpeg("http://img.youtube.com/vi/".$_GET['v']."/default.jpg");
// Defining the overlay image to be added or combined.
$insert = imagecreatefrompng("play.png");
// Select the first pixel of the overlay image (at 0,0) and use
// it's color to define the transparent color
imagecolortransparent($insert,imagecolorat($insert,0,0));
// Get overlay image width and hight for later use
$insert_x = imagesx($insert);
$insert_y = imagesy($insert);
// Combine the images into a single output image. Some people
// prefer to use the imagecopy() function, but more often than
// not, it sometimes does not work. (could be a bug)
imagecopymerge($background,$insert,0,0,0,0,$insert_x,$insert_y,100);
// Output the results as a png image, to be sent to viewer's
// browser. The results can be displayed within an HTML document
// as an image tag or background image for the document, tables,
// or anywhere an image URL may be acceptable.
imagepng($background,"",100);
?>
Do not close (avoid whitespaces or newslines) your script with ?> and use NULL instead "".
imagepng($background, NULL);
Then, in imagepng the quality parameter is between 0 and 9, as in http://it.php.net/manual/en/function.imagepng.php.

How to save a phpthumb output into a file?

I am trying to save PhpThumb output. As what I could find on-line was not sufficient or too complex, I would like to ask if any one knows how to it?
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
echo" '<'img src=".$thumb_src />";
So what I want to do is to save the img src into an Image.
(So far I was creating the thumbnails on the fly but it seems that google and my web server donĀ“t like it too much. Saving the thumbnails will ensure that in no time I will have all my thumbnails in real files and then I will use this function just for new content.)
From phpThumb's FAQ
The best way is to call phpThumb as an object and call RenderToFile() to save the
thumbnail to whatever filename you want. See /demo/phpThumb.demo.object.php for an example. The other way is to use the 'file' parameter (see /docs/phpthumb.readme.txt) but this parameter is deprecated and does not work in phpThumb v1.7.5 and newer.
Once you have generated the URL with this line you posted:
$thumb_src="\"phpThumb/phpThumb.php?src=../apartmentsPhotos/".$num['ref']."/1.JPG&h=119&q=100\"";
Pass it as a $_GET variable to another page, call it serveThumb.php:
if (!isset($_GET['img']))
exit;
header('Content-type: application/pdf');
echo file_get_contents($_GET['img']);
You might have to add your own validation to serveThumb.php. Now you can save the result of serveThumb.php as a JPG.
Alternatively, save the contents of the image as a JPG file.
if (!isset($_GET['img']))
exit;
$img = file_get_contents($_GET['img']);
file_put_contents("myImage.jpg", $img);

Have GD get image from binary string

I have a database that stores images in a MySQL BLOB field. I setup a script that selects and displays the images based on an ID in the URL, and I also made it so if you append ?resize=800x600, it would resize the image (in this case, to 800x600).
The host that I use doesn't have Imagemagick installed and won't let me do it myself, so I need to use PHP's GD library to resize the image.
But I've yet to find a function like Imagick's readImageBlob(), so I can't edit the binary string that I get from the database without first creating a temporary file, editing it, getting the binary string from it, sending it to the browser, and then deleting it (which is waaaay too many steps, especially since this will be getting a few thousand hits when it goes into production).
So my question is, is there any way to replicate readImageBlob with PHP's GD without going through the temporary file solution?
imagecreatefromstring() should do the trick. I think the function example in the manual is almost exactly what you need:
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
Where $data is your binary data string from the database.

Categories