I have done a code for images(more than 1 in single button click) upload while uploading image thumbnail wanna generate and save into a separete folder... in my code image uploading is working perfectly also first image of thumbnail also generating to the thumbnail folder, rest of the thumbnails are not generating.. throwing a error This is my image upload and creating a thumbnail code
$uploadDir = $_SERVER[DOCUMENT_ROOT].'/aqua/v_images/';
if(!empty($_FILES['img1']['name'])) {
$fileName1 = $_FILES['img1']['name'];
$tmpName1 = $_FILES['img1']['tmp_name'];
$fileSize1 = $_FILES['img1']['size'];
$fileType1 = $_FILES['img1']['type'];
$ext1 = substr(strrchr($fileName1, "."), 1);
$randName1 = md5(rand() * time());
$encFileName1 = $randName1.'.'.$ext1;
$filePath1 = $uploadDir . $encFileName1;
$result1 = move_uploaded_file($tmpName1, $filePath1);
if (!$result1) {
echo "Please Uploade a Image to Image 1 area";
exit;
}
if(!get_magic_quotes_gpc()) {
$fileName1 = addslashes($fileName1);
$filePath1 = addslashes($filePath1);
}
$thumb_name = $_SERVER[DOCUMENT_ROOT].'/aqua/v_thumb/'.$encFileName1;
$thumb=make_thumb($filePath1,$thumb_name,100,100);
}
////////////////////////////////////image2////////////////
if(!empty($_FILES['img2']['name'])) {
$fileName2 = $_FILES['img2']['name'];
$tmpName2 = $_FILES['img2']['tmp_name'];
$fileSize2 = $_FILES['img2']['size'];
$fileType2 = $_FILES['img2']['type'];
$ext2 = substr(strrchr($fileName2, "."), 1);
$randName2 = md5(rand() * time());
$encFileName2 = $randName2.'.'.$ext2;
$filePath2 = $uploadDir . $encFileName2;
$result2 = move_uploaded_file($tmpName2, $filePath2);
if(!get_magic_quotes_gpc()) {
$fileName2 = addslashes($fileName2);
$filePath2 = addslashes($filePath2);
}
$thumb_name = $_SERVER[DOCUMENT_ROOT].'/aqua/v_thumb/'.$encFileName2;
$thumb=make_thumb($filePath2,$thumb_name,100,100);
}
this is make_thumb function
<?php
function make_thumb($img_name,$filename,$new_w,$new_h)
{
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_img=imagecreatefromjpeg($img_name);
if(!strcmp("png",$ext))
$src_img=imagecreatefrompng($img_name);
//gets the dimmensions of the image
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
// next we will calculate the new dimmensions for the thumbnail image
// the next steps will be taken:
// 1. calculate the ratio by dividing the old dimmensions with the new ones
// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
//开发者_C百科 and the height will be calculated so the image ratio will not change
// 3. otherwise we will use the height ratio for the image
// as a result, only one of the dimmensions will be from the fixed ones
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
// we create a new image with the new dimmensions
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
// resize the big image to the new created one
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
// output the created image to the file. Now we will have the thumbnail into the file named by $filename
if(!strcmp("png",$ext))
imagepng($dst_img,$filename);
else
imagejpeg($dst_img,$filename);
//destroys source and destination images.
imagedestroy($dst_img);
imagedestroy($src_img);
}
// This function reads the extension of the file.
// It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}?>
showing errors in function make_thumb()
Warning: imagesx(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 14
Warning: imagesy(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 15
Warning: Division by zero in C:\AppServ\www\Aqua\thumb.php on line 32
Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\AppServ\www\Aqua\thumb.php on line 36
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 39
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 45
Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 48
Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\AppServ\www\Aqua\thumb.php on line 49
Change this line
$ext2 = substr(strrchr($fileName, "."), 1);
To this
$ext2 = substr(strrchr($fileName2, "."), 1);
Why don't you use http://pear.php.net/package/Image_Tools/docs/latest/Image_Tools/_Image_Tools-1.0.0RC1---Image---Tools---Thumbnail.php.html ?
精彩评论