I'm having a problem on a site I made where occasionally, a file I'm overlaying on another file somehow becomes the file saved.
Apparently I can't embed images or post more than two links, which makes it a bit difficult to explain what's going on. I made a demo page on my site with the images and the ImageMagick identify output. http://blha303.com.au/mcsanta/sodemo.html
And the relevant code:
$skinimage = file_get_contents("https://s3.amazonaws.com/MinecraftSkins/$user.png");
$fp = fopen(realpath(dirname(__FILE__)) ."/tmp/$user.png", "w");
fwrite($fp, $skinimage);
fclose($fp);
$santatemplate = imagecreatefrompng(realpath(dirname(__FILE__)) ."/SantaHatTemplate.png");
imageAlphaBlending($santatemplate, true);
imageSaveAlpha($santatemplate, true);
$userskin = imagecreatefrompng(realpath(dirname(__FILE__)) ."/tmp/$user.png");
imageAlphaBlending($userskin, true);
imageSaveAlpha($userskin, true);
imagecopy($userskin, $santatemplate, 0, 0, 0, 0, imagesx($userskin), imagesy($userskin));
imagepng($userskin, realpath(dirname(__FILE__)) ."/tmp/$user-santa.png");
https://github.com/blha303/mcsanta/blob/master/index.php#L38-L49
This is not an issue with transparency, I think it may be a problem with something about the PNG files, which is why I included the ImageMagick verbose output.
Any suggestions are greatly appreciated :)
Related
Having exhausted the possibility of solving the image resizing issue I posted in Problems rotating and resizing large images, due to having a shared server on which I can't alter the memory allowance or install additional image handling libraries, I wondered if there would be a way for me to use an external website to resize the image and pass it back to my PHP script?
Eg:
$mylargeimage = "http://www.mywebsite.com/uploads/largephoto.jpg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.imageresizingsite.com/resizethis.php?src=" . $mylargeimage . "&scale=50");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)
$output = curl_exec($ch);
curl_close($ch);
I don't know if this would work - nor have I found a website which actually resizes images - but is this approach a possibility and does anyone know of a website which would resize images using this kind of approach?
Try this if you want to resize the image yourself:
(i can't test the code right now, i haven't got a local server currently)
$image = $link_to_image;
$source_image = imagecreatefromfile($image);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$crop_measure = min($source_imagex, $source_imagey);
$to_crop_array = array('x' =>0 , 'y' => 0, 'width' => $crop_measure, 'height'=> $crop_measure);
$thumb_im = imagecrop($source_image, $to_crop_array);
imagejpeg($thumb_im, NULL, 80); //This may change, see the link below in this answer to see the right way to do that (it change because of extension)
}
?>
Link that maybe help you
Remember that if you need to resize images from php, you should have the gd driver installed on the server, to see if you have them, make a php_info() call in a empty page
IF YOU WANT TO DO THAT REMOTELY
THIS SHOULD HELP YOU
I'm trying to retrieve a remote image, but when I view the image (either output straight to browser or saved to disk and viewed) the quality is always lower.
Is there a way to do this without quality loss? Here are the methods I've used, but with the same result.
$imagePath is a path like http://www.example.com/myimage.jpg and $filePath is where the image will be saved to.
curl
$ch = curl_init($imagePath);
$fp = fopen($filePath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
file_get_contents
$tmpImage = file_get_contents($imagePath);
file_put_contents($filePath, $tmpImage);
I'll get a screenshot to show the quality issues shortly.
If you look around the KIA logo you can see of the left the quality issues I'm having.
Update:
Upon some further testing with different images it seems the quality issues are different with each image. Some has no issues at all.
Also the image which the screenshots are from above, based on the long url to the image, it seems like the image has already been resized and had it's quality alter before it gets to my script, so I'm thinking that could account for these issues too.
My goal here is to be able to pull an image out of a url on my server, rotate that image by 90 degrees, then save it back to the same url I pulled it from.
I have tried many different methods of this but nothing is working.
This is what I have currently:
$imurl = "../images/sample.jpg";
$file = fopen($imurl, "rb");
$rotim = imagerotate($file, 90, 0);
move_uploaded_file($rotim, $imurl);
I am not sure if this changes anything but the pictures url is not in the same directory as the current file.
I have been alternating between these two link formats - neither seem to work.
$imurl = "https://www.site.com/images/1/picture.jpg"
$imurl = "../images/1/picture"
your 90% there..
$imurl = "../images/sample.jpg";
$file = imagecreatefromjpeg($imurl); //http://nz2.php.net/manual/en/function.imagecreatefromjpeg.php
$rotim = imagerotate($file, 90, 0); //http://nz2.php.net/manual/en/function.imagerotate.php
imagejpeg($rotim, $imurl); //http://nz2.php.net/manual/en/function.imagejpeg.php
be careful with relative paths, i prefer to use the full path where possible.
I have a fairly basic PHP script used to generate an image by combining several files, using variables stored in the URL, into one. For ease of explanation, here it is:
<?php
$images = array( $_GET['item1'], $_GET['item2'], $_GET['item3'] );
// Allocate new image
$img = imagecreatetruecolor(480, 480);
// Make alpha channels work
imagealphablending($img, true);
imagesavealpha($img, true);
foreach($images as $fn) {
// Load image
$cur = imagecreatefrompng($fn);
imagealphablending($cur, true);
imagesavealpha($cur, true);
// Copy over image
imagecopy($img, $cur, 0, 0, 0, 0, 480, 480);
// Free memory
imagedestroy($cur);
}
header('Content-Type: image/png'); // Comment out this line to see PHP errors
imagepng($img);
?>
No issue with that.
I don't really understand what to do with this now though. I would like to download this to the client's downloads folder but I am unaware of and cannot find any methods for this.
I do not particularly need someone to do this for me, but just a push in the right direction for resources on doing this.
I know how to do this by temporary storing that file as a .png on my server and directing the user to a download link for that but that seems like the long way around this.
Any help please?
-Tim
You should save your code within a php file i.e. image.php. If a user calls this file passing valid arguments like this
www.example.com/image.php?item1=bild1.png&item2=bild2.png&item3=bild3.png
the file will be displayed within the browser (you don't have to save it on server side). To force a download of the image add an additional header:
header('Content-Disposition: Attachment;filename=image.png');
I am using Google Charts from a URL for example:
http://chart.apis.google.com/chart?cht=lc&chs=250x100&chds=0,20...
How do I go about using PHP to save the image. I have tried:
$image = file_get_contents($lineChart->getUrl());
file_put_contents('playerchart.png', $image);
and
$ch = curl_init($lineChart->getUrl());
$fp = fopen('playerchart.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
However, both seem to corrupt the image and the image ends up not working.
Any ideas? Thanks
I tested the following code and I got a proper PNG that I could open in Preview.
$image = file_get_contents('http://chart.apis.google.com/chart?cht=lc&chs=250x10
0&chds=0,20');
file_put_contents('playerchart.png', $image);
Given that the above works, I would say that there is fair chance that there is an issue with the $lineChart->getURL() and it might not be returning exactly what you expect. (I'd say print it out to the screen and double check, there might be some other characters or which space or the like in there. The 'image' that you are saving to disk might actually be the HTML for a 404 page!)
If you'd like an alternative way of saving the file, I would suggest the below. This will fail if the destination URL is not an image.
$im = imagecreatefrompng($theurl);
imagepng ($im, 'mypic.png');
imagedestroy($im);
This works for me:
<?php
$img = file_get_contents("http://chart.apis.google.com/chart?cht=lc&chs=250x100&chds=0,20");
file_put_contents("test.png", $img);
?>