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);
?>
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'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 want to download multiple images from the following website:
www.bbc.co.uk
I want to do it by using PHP cURL, can someone help lead me in the right direction?
It would be nice to download all the images in one shot, but if someone can help me download maybe download just 1 or a bunch that would be great!
Edit: it would be a good idea to show what I have tried:
<?php
$image_url = "www.bbc.co.uk";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
// output to browser
header("Content-type: image/jpeg");
print $image;
?>
For some reason it is not working. It is to be noted I am an absolute amatuer at PHP and programming in general.
The above code you pasted isn't doing what you think it is.
$image = curl_exec($ch);
The $image variable doesn't contain any image, it actually contains the entire HTML of that webpage as a string.
If you replace
// output to browser
header("Content-type: image/jpeg");
print $image;
with:
var_dump($image);
You will see the html.
Something like this:
Try to find the actual champion image source and parse it accordingly
You just need the whole /Air/assets/images/champions folder right?
Nothing easier when you use FireFox and a Download plugin like "Download Them All", open the FTP folder (Eambo's link) where the champions' pictures are located, right clic, select the plug-in.
It's gonna list all the files, select them all or select those you need and start the download.
Also, if you own that game you can take a look in this path:
\League of Legends\rads\projects\lol_air_client\releases\<newest-version>\deploy\assets\images\champions
Since you will probably use that in your university, CHECK THIS and I hope you will find a solution.
I know my question is quite similar to this question and I'm using the answer to the problem as my solution but I can't get it to work.
I'm using Immediatenet.com to generate a thumbnail of any given URL (example: google). And then using cURL to save the image in a folder on my server.
I'm not sure if the problem lies in the fact that the URL isn't actually an image URL (ie example.com/image.jpg) or a .html/.php URL or that I'm simply not doing it right. I've never used cURL before so it's distinctly possible that I'm not doing it right.
I'm saving the path to the image into a MySQL database which works just fine. However, the image doesn't actually save in the folder specified. The permissions are set correctly on the folder, so I don't think it's a permissions problem. I also set allow_url_fopen to On just to make sure.
My code is below. Can anyone point me in the right direction for this one?
$url = $_POST['url'];
$url_new = 'http://immediatenet.com/t/m?Size=1024x768&URL='.$url;
$img = "/users/images/".$url.".jpg";
$ch = curl_init($url_new);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
If $url contains forward slashes (as most URLs do), these will be treated as directory delimiters in the $img pathname. You either need to replace the slashes with some other character, or create all the necessary directories.
$img = "/users/images/".str_replace('/', '_', $url).".jpg";
Are you sure $img = "/users/images/".$url.".jpg"; is valid? .. do you really have a users folder in your root?
Please correct that if not and it should be ok.
I'm using imagecreatefromjpeg() function to merge two pictures..
now the problem which I'm facing is that when I use the pictures from my server, it works perfectly and when I use pictures from some other website, it doesn't work.
For example: when I use this PHP file http://coolfbapps.in/test/merger.php with function
imagecreatefrompng('http://coolfbapps.in/test/1.png');
It works perfectly fine as the image is at my own server
but when I alter this function n put the link of an image which is not on my server,
for example.
imagecreatefrompng('http://www.businesseconomics/Test.png');
it doesnt work. (the image file is not on my server)
please suggest me an alternative to this function or a solution as I want to use this with Facebook apps..
Functions like file-get-contents are also showing the same error. I hope its not server end problem..
allow_url_fopen is on but
allow_url_include is off
Update...Actual code. I'm using this to merger two pictures
$dest = imagecreatefrompng('http://coolfbapps.in/test/1.png');
$src = imagejpeg('http://img.allvoices.com/thumbs/image/111/111/75152279-pic.jpg');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
Instead of using file_get_content you can use cURL to get your image data. Here is a resource:
http://php.net/manual/en/book.curl.php
Example with getting html ( images will also work ):
<?php
$ch = curl_init("http://img.allvoices.com/thumbs/image/111/111/75152279-pic.jpg");
$fp = fopen("example_homepage.jpg", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$img = imagecreatefromjpeg("example_homepage.jpg");
?>
Sounds like the function does not have URL opening capabilities, or it does and you have allow_url_fopen off in php.ini. You can't use ini_set() for security reasons.
You could download the file to your local server, and then open it.
file_put_contents('image.jpg',
file_get_contents('http://www.businesseconomics/Test.png')
);
You could probably use copy() too, the docs hint that it can read URLs.
Something like this might help.
$imagestr = file_get_contents('http://www.businesseconomics/Test.png');
$image = imagecreatefromstring($imagestr);
imagecreatefrompng($image);
UPDATED::
$imagestr = file_get_contents('http://www.gravatar.com/avatar/95111e2f99bb4b277764c76ad9ad3569?s=32&d=identicon&r=PG');
$image = imagecreatefromstring($imagestr);
header('Content-type: image/jpeg');
imagejpeg($image);