I have some code that will upload a jpeg image through a form, scale it to a specific size, save it to my server and enter the file name into a mySQL table. This all works perfectly. The problem comes when I try to also create a thumbnail from the image that I just uploaded and save it into a /thumbs directory. Here is the function that does the resize:
Code: Select all
<?php
$imgsize = getimagesize($userfile1);
if(($imgsize[0] > 350 || $imgsize[1] > 350))
{
$tmpimg = tempnam("/tmp","MKUP");
// resize large image
system("djpeg $userfile1 > $tmpimg");
system("pnmscale -xy 350 350 $tmpimg | cjpeg -smoo 10 -qual 90 >$userfile1");
unlink($tmpimg);
}
// process the image file uploaded by the form
$safefilename = ereg_replace("[^A-Za-z0-9._]", "",str_replace(" ", "_",strtolower($_FILES['userfile1']['name'])));
$destfilepath = $imagepath.$safefilename;
$destfilepath_thumb = $imagepath.'thumbs/t_'.$safefilename;
// move the image file to the directory on the server
if (!move_uploaded_file($_FILES['userfile1']['tmp_name'], $destfilepath))
{
echo "<p><strong>There was a problem uploading your image</strong>.</p>";
exit;
}
// $destfilepath = "/home/rightons/public_html/client/staging/levonian/gallery/uploaded_images/body_center_image.jpg" ** these are the values that are being passed into the function, shown here for information only
// $destfilepath_thumb = "/home/rightons/public_html/client/staging/levonian/gallery/uploaded_images/thumbs/t_body_center_image.jpg"
createthumb($destfilepath,$destfilepath_thumb,60,60);
function createthumb($name,$filename,$new_w,$new_h){
echo $name;
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$gd2 = "yes"; // force imagecopyresampled
if ($gd2==""){
$dst_img=ImageCreate($thumb_w,$thumb_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
}else{
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
}
if (preg_match("/png/",$system[1])){
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
?>Please, any help would be very appreciated. It shouldn't be that difficult to make a thumbnail! Aargh!!