PHP ImageCreateFromString and file_get_contents
I am trying to make a function in PHP that will allow me to enter basically any URL and then runs some functions on it just as if a user was uploading on my server. SO I will resize and make some thumbnails but I need help just getting the image in a state that I can run my other codes on it. Another user on this site helped me get started with ImageCreateFromString() and file_get_contents()
Please note this code is missing a lot of stuff I am aware of, I am just trying to get the basic function working and then I will add in all the security measures
I tried this code below using a URL like this with the photo URL added to my script url:
开发者_JAVA技巧http://example.com/friendproject2/testing/photos/fromurl/?url=http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png
But it shows nothing and not even an error
function getphotoURL($url){
if(isset($url) && $url != 'bad') {
$image = ImageCreateFromString(file_get_contents($url));
if (is_resource($image) === true){
echo 'The URL of the image we fetch is :' .$url. '<BR><BR>';
//show image
header('Content-Type: image/jpeg');
imagejpeg($image, null, 100);
imagedestroy($image);
imagedestroy($image);
// image is valid, do your magic here
}else{
// not a valid image, show error
echo 'error getting URL photo from ' .$url;
}
}else{
//url was empty
echo 'The URL was not passed into our function';
}
}
?>
###### UPDATE #####
It seems it was a simple error on my part, something simple as checking for a POST request instead of a GET request, here is my new code below.
I have a couple of issues,
1) I am using imagejpeg($image, null, 100); and I am wondering, should I be using something else? Does it require the source image to be a jpg or will it work with any image? I need to allow the main types (jpg, jpeg, gif, png)
2) same as above question but for when showing the image on screen I have header set to this: header('Content-Type: image/jpeg'); should it not be jpg for other type of images?
3) Is there a way that I can make sure that the source URL passed in is an actual image and do whatever I want if it is not a image, like show my own error or do my own code once it detect that the URL is not a valid image url
<?PHP
// run our function
if(isset($_GET['url']) && $_GET['url'] != "") {
getphotoURL($_GET['url'],'no');
}
function getphotoURL($url, $saveimage = 'yes'){
if(isset($url) && $url != '') {
$image = imagecreatefromstring(file_get_contents($url));
if (is_resource($image) === true){
if($saveimage === 'yes'){
// resize image and make the thumbs code would go here if we are saving image:
// resize source image if it is wider then 800 pixels
// make 1 thumbnail that is 150 pixels wide
}else{
// We are not saving the image show it in the user's browser
header('Content-Type: image/png');
imagejpeg($image, null, 100);
imagedestroy($image);
}
}else{
// not a valid resource, show error
echo 'error getting URL photo from ' .$url;
}
}else{
// url of image was empty
echo 'The URL was not passed into our function';
}
}
?>
After calling imagecreatefromstring() for a PNG or GIF (For transparency)
Do the following manipulations on the image:
imagealphablending($image, true); // setting alpha blending on
imagesavealpha($image, true);
Will turn the flat black background to the alpha channel.
In response to your new questions:
1) I am using imagejpeg($image, null, 100); and I am wondering, should I be using something else? DOes it require the source image to be a jpg or will it work wiht any image? I need to allow the main types (jpg, jpeg, gif, png)
Well, php.net says, "imagejpeg() creates a JPEG file from the given image". But the important part is this, "An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().". And your using "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." So, that should be ok.
2) same as above question but for when showing the image on screen I have header set to this: header('Content-Type: image/jpeg'); should it not be jpg for other type of images?
The header should be of type jpg - If that's the file type, then you're correct.
3) Is there a way that I can make sure that the source URL passed in is an actual image and do whatever I want if it is not a image, like show my own error or do my own code once it detect that the URL is not a valid image url
Yeah - Instead of doing:
$image = ImageCreateFromString(file_get_contents($url));
You could do:
$image = imagecreatefromjpeg($url);
if (!$image) echo "error";
imagecreatefromjpeg() returns an image identifier representing the image obtained from the given filename.
But really, what you have is fine.
Does it display the error message
echo 'The URL was not passed into our function';
Or nothing at all?
If the error messaging is being displayed, possible the check === is failing:
An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded.
Also, do you have error logging maxed out on your development server? That way you can see any possible warnings being thrown?
精彩评论