dsadsadsa1231 2013-05-21 07:47
浏览 32
已采纳

方形缩略图前进

I have a php thumbnail function. How it works you can check below:

public static function makeThumb($source, $destination, $thumb_width){

        $size   = getimagesize($source);
        $width  = $size[0];
        $height = $size[1];
        $x      = 0;
        $y      = 0;

        $status  = false;

        if ($width > $height) {
            $x      = ceil(($width - $height) / 2);
            $width  = $height;
        } else if ($height > $width) {
            $y      = ceil(($height - $width) / 2);
            $height = $width;
        }

        $new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die ('Cannot Initialize new GD image stream');
        $extension = self::get_file_extension($source);
        if ($extension == 'jpg' || $extension == 'jpeg')
            $image = imagecreatefromjpeg($source);
        if ($extension == 'gif')
            $image = imagecreatefromgif($source);
        if ($extension == 'png')
            $image = imagecreatefrompng($source);

        imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);


        if ($extension == 'jpg' || $extension == 'jpeg')
            $status = @imagejpeg($new_image, $destination);
        if ($extension == 'gif')
            $status = @imagegif($new_image, $destination);  
        if ($extension == 'png')
            $status = @imagepng($new_image, $destination);      

        imagedestroy($image);

        return $status;
    }

Please check images below (how it works):

Original imageThumbnail image result

Question: How can I get this image as a result (This thumb is from photoshop)?

I need this thumb

  • 写回答

2条回答 默认 最新

  • dsrruefh12970 2013-05-22 10:27
    关注

    The right solution is here:

    public static function makeThumb($source, $destination, $square_size=167, $quality=90) {
    
            $status  = false;
            list($width, $height, $type, $attr) = getimagesize($source);
    
            if($width> $height) {
               $width_t =  $square_size;
               $height_t    =   round($height/$width*$square_size);
               $off_y       =   ceil(($width_t-$height_t)/2);
               $off_x       =   0;
    
            } elseif($height> $width) {
    
               $height_t    =   $square_size;
               $width_t =   round($width/$height*$square_size);
               $off_x       =   ceil(($height_t-$width_t)/2);
               $off_y       =   0;
    
            } else {
    
                $width_t    =   $height_t   =   $square_size;
                $off_x      =   $off_y      =   0;
            }
    
            $thumb_p    = imagecreatetruecolor($square_size, $square_size);
    
            $extension  = self::get_file_extension($source);
    
            if($extension == "gif" or $extension == "png"){
    
                imagecolortransparent($thumb_p, imagecolorallocatealpha($thumb_p, 0, 0, 0, 127));
                imagealphablending($thumb_p, false);
                imagesavealpha($thumb_p, true);
            }   
    
            if ($extension == 'jpg' || $extension == 'jpeg')
                $thumb = imagecreatefromjpeg($source);
            if ($extension == 'gif')
                $thumb = imagecreatefromgif($source);
            if ($extension == 'png')
                $thumb = imagecreatefrompng($source);
    
            $bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
            imagefill ($thumb_p, 0, 0, $bg);
    
            imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
    
            if ($extension == 'jpg' || $extension == 'jpeg')
                $status = @imagejpeg($thumb_p,$destination,$quality);
            if ($extension == 'gif')
                $status = @imagegif($thumb_p,$destination,$quality);
            if ($extension == 'png')
                $status = @imagepng($thumb_p,$destination,9);
    
            imagedestroy($thumb);
            imagedestroy($thumb_p);
    
            return $status;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100