Tutorial: How to make an Image Upload and Thumbnailer Script with cURL
3. Now lets examine the changes in the PHP coding. First up where we tested for MIME types instead of file extensions:
-
} else {
-
$filetype = $_FILES['srcimage']['type'];
-
}
As you can see this section is much fatter than before. Unfortunately, as described above, unless you have some PHP extensions installed (which if you are like me and are on shared hosting won't happen) you will need to determine the file type by trusting the file extension.
The first line if(strlen($_POST['srcurl']) > 0){ looks to see if the user is using the address field to upload an image, or whether they are uploading a file with the form, the address field has taken priority and is tested first, meaning, if the user has entered both text and selected a file locally then the address bar image will be selected. You can remove this issue via a radio button selection tool.
The script then sees if the entries are in the given arrays, if it is it proceeds.
4. The next change occurs when the file needs to be moved. In this case the file must be fetched, named, moved, saved. This happens here:
-
$grab = curl_init();
-
curl_setopt($grab, CURLOPT_URL, $_POST['srcurl']);
-
curl_setopt($grab, CURLOPT_HEADER, false);
-
curl_setopt($grab, CURLOPT_RETURNTRANSFER, true);
-
$grabbed = curl_exec($grab);
-
curl_close($grab);
-
} else {
-
$filename = strtolower(md5((rand() * time()).$_SERVER['REMOTE_ADDR']).strrchr($_FILES['srcimage']['name'], '.'));
-
}
It first checks if the file upload method is using an external file. If it is it handles the file then we come to the only cURL code we need:
-
$grab = curl_init();
-
curl_setopt($grab, CURLOPT_URL, $_POST['srcurl']);
-
curl_setopt($grab, CURLOPT_HEADER, false);
-
curl_setopt($grab, CURLOPT_RETURNTRANSFER, true);
-
$grabbed = curl_exec($grab);
-
curl_close($grab);
The first line initialises a cURL object. The second line curl_setopt($grab, CURLOPT_URL, $_POST['srcurl']); sets the URL option of the cURL object to whatever was entered into form. The third line sets the cURL output to not include the HTML header file, which would complicate way we handle the image. The 4th line does something similar, but sets the output to be stored into a string as the default is to be printed to the screen. The 5th line tells cURL to start the transfer, and store the fetched data into the $grabbed variable. Once done close the cURL object. Easy huh?
So back to the code above, if the upload method isn't via a link, then use the method we had in the previous tutorial, the upload method.
5. The only other change is where we choose what canvas to begin from.
-
switch ($filetype){
-
case '.jpg':
-
$srcimage = imagecreatefromjpeg($tmppath.$filename);
-
break;
-
case '.jpeg':
-
$srcimage = imagecreatefromjpeg($tmppath.$filename);
-
break;
-
case '.gif':
-
$srcimage = imagecreatefromgif($tmppath.$filename);
-
break;
-
case '.png':
-
$srcimage = imagecreatefrompng($tmppath.$filename);
-
break;
-
default:
-
$srcimage = imagecreatefromjpeg($tmppath.$filename);
-
}
-
} else {
-
switch ($filetype){
-
case 'image/jpeg':
-
$srcimage = imagecreatefromjpeg($tmppath.$filename);
-
break;
-
case 'image/gif':
-
$srcimage = imagecreatefromgif($tmppath.$filename);
-
break;
-
case 'image/png':
-
$srcimage = imagecreatefrompng($tmppath.$filename);
-
break;
-
default:
-
$srcimage = imagecreatefromjpeg($tmppath.$filename);
-
}
-
}
As you might already recognise the first line tests what method of upload we are using. Then it executes the case statement according to the method. The key difference here is that if we are using the cURL method, we are determining which canvas to use on the basis of file extensions. Whereas the fileupload method is based on MIME types. I don't need to repeat myself again as to why we are using this method.
From there our script just continues to handle the files stored on the server, so it doesn't really make any difference what method you are using beyond this point. That's it! now run along and play!

One Response to “Tutorial: How to make an Image Upload and Thumbnailer Script with cURL”
By ghprod
on Aug 17, 2008
Hi … thnx 4 great script