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.
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";`
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 encrypting imagea into base64 then storing them in the database. I then print the images using PHP, but sometimes I get a corrupted image. If I put the same code in an HTML file, or if I refresh the page many times, then it works.
This is my corrupted image:
My HTML looks like this:
<img src="data:image/png;_encrypteddata_" />
NOTE: _encrypteddata_ is my encrypted image (I cannot post that huge data here)
It works fine, but sometimes it shows a continuously corrupted image with the same data. Is it having problem with browser or base64?
I'm using image/png for all the icons. Would that cause any problems?
I think it's coming from the browser.
NB :Retreiving image data from the database on every page load can be slow.
Try writing an image file on your filesystem with the data and link to this file in your HTML. It will be faster and more robust.
Basically base64 get much memory for storing encrypted data especially images so whenever you get that little bit huge data from database it takes some times to be loaded . sometimes browser does not wait much time to decrypt so you could see corrupted images.
You better to store your images in the local file system rather than storing into the database. it will speed up your process.
use this code for store your data into local file system
function get_image($image_url, $localPathToStore)
{
echo $url . "<br>" . $saveto;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw = curl_exec($ch);
curl_close($ch);
if (file_exists($saveto)) {
unlink($saveto);
}
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);
}
I got this solution
<img src='data:image/;base64,_encrypteddata_'/>
From http://www.kingpabel.com/php-base64_encode/
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'm willing to use thumbnails into my website which is mainly like websites directory.
I've been thinking to save url thumbnails into certain directory !
Example :-
I'm going to use free websites thumbnails service that gives me code to show thumbnail image of any URL as follow
<img src='http://thumbnails_provider.com/code=MY_ID&url=ANY_SITE.COM'/>
This would show the thumbnail of ANY_SITE.COM
i want to save the generate thumbnail image into certain directory my_site.com/thumbnails
Why i'm doing this ?
in fact my database table is like my_table {id,url,image} where i'm going to give the image thumbnail random name and store its new name into my_table related to its url then i can call it back anytime and i know how to do it but i don't know how to save it into certain directory.
any help ~thanks
Using cURL should work for you:
$file = 'the URL';
$ch = curl_init ($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
$fullpath = 'path to destination';
$fp = fopen($fullpath);
fwrite($fp, $rawdata);
fclose($fp);
You could use curl to fetch the remote image. You can save it with curl_setopt($handler, CURLOPT_FILE, '/my/image/path/here.jpg');. The id could be something simple like a hash of the original URL. Obviously you'd have to check to make sure the directories exist before you save the file (using is_dir() and creating them with mkdir() if they don't).