php图片上传没有创建缩略图

时间:2012-09-29 19:19:20

标签: php file-upload thumbnails image-uploading

嘿,我需要对此代码提供一些帮助,以便上传正确尺寸的图片以及相应的缩略图:

    $file_path = $this->options['upload_dir'].$file_name;
    $new_file_path = $options['upload_dir'].$file_name;
    list($img_width, $img_height) = @getimagesize($file_path);

    if (!$img_width || !$img_height) {
        return false;
    }

    $scale = min(
        $options['max_width'] / $img_width,
        $options['max_height'] / $img_height
    );

    if ($scale > 1) {
        $scale = 1;
    }

    $new_width = 1280; //$new_width = $img_width * $scale;
    $new_height = 323; //$new_height = $img_height * $scale;        
    $new_img = @imagecreatetruecolor($new_width, $new_height);

    switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
        case 'jpg':
        case 'jpeg':
            $src_img = @imagecreatefromjpeg($file_path);
            $write_image = 'imagejpeg';
            break;
        case 'gif':
            $src_img = @imagecreatefromgif($file_path);
            $write_image = 'imagegif';
            break;
        case 'png':
            $src_img = @imagecreatefrompng($file_path);
            $write_image = 'imagepng';
            break;
        default:
            $src_img = $image_method = null;
    }

    $success = $src_img && @imagecopyresampled(
        $new_img,
        $src_img,
        0, 0, 0, 0,
        $new_width,
        $new_height,
        $img_width,
        $img_height
    ) && $write_image($new_img, $options['upload_dir'] . $_GET['type'] . '.' . strtolower(substr(strrchr($file_name, '.'), 1)), 100);

    @imagedestroy($src_img);
    @imagedestroy($new_img);

我试图添加这个:

    $success = $src_img && @imagecopyresampled(
        $new_img,
        $src_img,
        0, 0, 0, 0,
        192,
        50,
        $img_width,
        $img_height
    ) && $write_image($new_img, $options['upload_dir'] . $_GET['type'] . 'THUMB.' . strtolower(substr(strrchr($file_name, '.'), 1)), 100);

但它只是复制相同的图像两次,其高度和宽度与第一张相同:

Bob.jpg         800kb
BobTHUMB.jpg    800kb

1 个答案:

答案 0 :(得分:2)

$thm_img = @imagecreatetruecolor( 192, 50 );

$success = $src_img && @imagecopyresampled(
   $thm_img,
   $src_img,
   0, 0, 0, 0,
   192,
   50,
   $img_width,
   $img_height
) && $write_image($thm_img, $options['upload_dir'] . $_GET['type'] . 'THUMB.' . strtolower(substr(strrchr($file_name, '.'), 1)), 100);