图片缩略图问题

发布于 2024-10-19 08:29:15 字数 4939 浏览 3 评论 0原文

我已经完成了图像代码(单击按钮超过 1 个)上传,同时上传图像缩略图想要生成并保存到单独的文件夹中...在我的代码中,图像上传工作正常,缩略图的第一张图像也生成到缩略图文件夹,其余缩略图未生成..抛出错误 这是我的图像上传和创建缩略图代码,

$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);
}

这是 make_thumb 函数,

<?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
    //      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;
 }?> 

显示函数 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

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
    //      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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2024-10-26 08:29:15

更改此行
$ext2 = substr(strrchr($fileName, "."), 1);

到此
$ext2 = substr(strrchr($fileName2, "."), 1);

Change this line
$ext2 = substr(strrchr($fileName, "."), 1);

To this
$ext2 = substr(strrchr($fileName2, "."), 1);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文