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
Related
So I've been using this approach to resize images uploaded via forms in PHP:
list ($width, $height, $type, $w)=getimagesize($_FILES[$imageName]['tmp_name']);
$info = getimagesize($_FILES[$imageName]['tmp_name']);
This works well - allows resizing & conversion to png.
Now I need to do the same thing - but for downloaded images, e.g. given the url of an image online such as http://colorvisiontesting.com/images/plate%20with%205.jpg.
From the looks of it this can be done with CURL, but I can't quite work out how to then create an image object from it. This is what I have so far:
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_URL, $imageURL);
$curlImage = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
list ($width, $height, $type, $w)=getimagesize($curlImage);
$info = getimagesize($curlImage);
But this is failing at getimagesize - and I can't work out what the correct approach is here. Any ideas?
The getimagesize function works on filename (local or remote) but not on a string which represents the content of your image.
You can:
Save that content to a local file (using file_put_contents, for example)
Use the getimagesize function on the remote file (it should work).
Use the getimagesizefromstring (which works the same as getimagesize but works in data and not filename)
Use the imagecreatefromstring (which you need if you want to convert your stream/data to image (gd) resource, and then use the imagesx and imagesy on the resouce.
I am writing a script where I am just resizing images from the requested image url and caching them. I am also giving width & height as optional params for the request. I am caching the images by their filenames & I want to cover one use case when user requests the same image with different width or height.
Since I am using Codeigniter's image resizing library, the filenames are appended with _thumb. That's how I am storing the images.
What should I do in that case? One solution would be to check the md5 hash of the resized image and cached image.
Here's what I am thinking to do:
$ch = curl_init($url); //initialize cURL
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$dataToWrite = curl_exec($ch); //Execute the given cURL session.
curl_close ($ch); //close the given cURL session
$fp = fopen($dirPath.$filename,'w');
fwrite($fp, $dataToWrite);
fclose($fp);
Finally,
if (md5_file($dirPath.$filename) == md5_file($dirPath.$resizedImage){
serveimagefromcache() & deletethedownloadedfile();
} else {
resizetheimage() && serveit();
}
I am concerned about the performance as well, I would appreciate any other suggestions.
Even though #arkascha's suggestion was quite good but saving the file as filename hash is much more better.
With this,
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$filehash = sha1($filename.$width.$height);
$filename = $filehash.".".$ext
you can prevent the issue that I was having.
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.
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 :)
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);
?>