douju4278 2012-09-29 19:19
浏览 23
已采纳

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

Hey all i am in need of some help with this code in order for it to upload the correct size image plus a corresponding thumbnail for it:

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

I've tried to add this:

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

But it just copies the same image twice with the same height and width as the first one:

Bob.jpg         800kb
BobTHUMB.jpg    800kb
  • 写回答

1条回答 默认 最新

  • dtol41388 2012-09-29 19:30
    关注
    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码