Help with PHP ImageCreateFromString and file_get_contents

id1985

New Member
I am trying to make a function in PHP that will allow me to enter basicly 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 urlhttp://localhost/friendproject2/tes...m/a/1262802780/images/twitter_logo_header.pngBut it shows nothing and not even an error
\[code\]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 funtion'; }}?>\[/code\]###### 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 wiht 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\[code\]<?PHP// run our functionif(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 funtion'; }}?>\[/code\]
 
Back
Top