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);
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.
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 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'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);
?>