【问题标题】:Image thumbnail problem图片缩略图问题
【发布时间】:2011-07-04 07:40:13
【问题描述】:

我已经完成了图像代码(单击按钮超过 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

【问题讨论】:

  • 你的问题解决了吗?
  • 还没有?如果你能帮助我,请帮忙
  • @user618124:看看我更新的答案

标签: php gd thumbnails


【解决方案1】:

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

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

【讨论】:

  • 但第一张图片正在上传并完美生成缩略图!这就是我无法理解的......我错了!
  • 我已经纠正了这个错误!但还是有同样的问题!!
  • 兄弟,我真的很抱歉你的正确,因为我的浏览器保留了现金,所以做了这么多的工作@最后告诉我仪式路径 thaxx 很多
【解决方案2】:
猜你喜欢
  • 2012-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
相关资源
最近更新 更多