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.
Related
So I found the perfect example of a page I would like to download images from, this so happens to be http://www.habbo.com/habbo-imaging/avatarimage?figure=ch-215-110.hd-180-7.lg-275-110.hr-893-61&direction=3&head_direction=3&headonly=1&gesture=sml&size=1
Now, when you go to save the image if you were to on your desktop, it reads as a PNG file, although I am trying to save it using PHP but I want it to save as GIF.
What I've so far is:
$ch = curl_init('https://www.habbo.com/habbo-imaging/avatarimage?figure=hr-125&direction=3&head_direction=3&headonly=1&gesture=sml&size=1');
$fp = fopen('game/c_images/badges/' . $badge_id . '.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
I managed to get the permissions working and a image saves, but it's saving just the file name and the photo isn't there. So I'm guessing it has something to do with me saving a PNG file as a GIF, surely there's something I'm missing.
yes,You can save image using html dom.
like use
`$new_image_name = '2018_'.mt_rand();
$base_url = 'https://www.habbo.com/habbo-imaging/avatarimage?figure=hr-125&direction=3&head_direction=3&headonly=1&gesture=sml&size=1';
$img = "./image_path/$new_image_name.jpg";
file_put_contents($img,file($base_url));
$local_image_url = "http://example.org/test/path_to_image/$new_image_name.jpg";`
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 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 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);
?>
I want to be able to retrieve a remote image from a webserver, resample it, and then serve it up to the browser AND save it to a file. Here is what I have so far:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "$rURL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$out = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$imgRes = imagecreatefromstring($out);
imagejpeg($imgRes, $filename, 70);
header("Content-Type: image/jpg");
imagejpeg($imgRes, NULL, 70);
exit();
Update
Updated to reflect correct answer based on discussion below
You can fetch the file into a GD resource using imagecreatefromstring():
imagecreatefromstring() returns an image identifier representing the image obtained from the given data. These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.
from there, it's pretty straightforward using imagecopyresampled().
Output it using imagejpeg() or whichever output format suits you best.
Make one output with a file name:
imagejpeg($image_resource, "/path/to/image.jpg");
then send the same resource to the browser:
header("Content-type: image/jpeg");
imagejpeg($image_resource);
depending on the image's format, you may want to use the imagepng() or imagegif() functions instead.
You may want to work with different output formats depending on the input file type. You can fetch the input file type using getimagesize().
Remember that you can adjust the resulting JPEG image's quality using the $quality parameter. Default is 75% which can look pretty crappy.