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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line