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:
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';
}
}
?>
In response to your new questions:
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.
The header should be of type jpg – If that’s the file type, then you’re correct.
Yeah – Instead of doing:
You could do:
But really, what you have is fine.
Does it display the error message
Or nothing at all?
If the error messaging is being displayed, possible the check === is failing:
Also, do you have error logging maxed out on your development server? That way you can see any possible warnings being thrown?