【问题标题】:Crop center square of image using imagecopyresampled使用 imagecopyresampled 裁剪图像的中心正方形
【发布时间】:2015-02-05 16:22:15
【问题描述】:

我正在尝试从正常大小的图像创建 2 个裁剪的缩略图(60x60 和 150x150)。除了裁剪之外,一切都很好,这不起作用。该部分正在生成一个缩略图,右侧有一条黑线,如下所示:

.............
.         ...
.         ...
.         ...
.............

调整大小效果很好,因为最终缩略图的正确高度为 60 和 150(从风景图像作为原始图像),但它们仍然是风景。

我有什么:

list($thumb_width, $thumb_height) = getimagesize($thumb_target);
if ($thumb_width > $thumb_height) { // landscape
    $thumb1_new_height = 60;
    $thumb1_new_width = floor( $thumb_width * ( $thumb1_new_height / $thumb_height ));
    $thumb1_crop_x = ceil(($thumb_width - $thumb_height) / 2);
    $thumb1_crop_y = 0;
    $thumb2_new_height = 150;
    $thumb2_new_width = floor( $thumb_width * ( $thumb2_new_height / $thumb_height ));
    $thumb2_crop_x = ceil(($thumb_width - $thumb_height) / 2);
    $thumb2_crop_y = 0;
}
else if ($thumb_width < $thumb_height){ // portrait
    $thumb1_new_width = 60;
    $thumb1_new_height = floor( $thumb_height * ( $thumb1_new_width / $thumb_width ));
    $thumb1_crop_x = 0;
    $thumb1_crop_y = ceil(($thumb_height - $thumb_width) / 2);
    $thumb2_new_width = 150;
    $thumb2_new_height = floor( $thumb_height * ( $thumb2_new_width / $thumb_width ));
    $thumb2_crop_x = 0;
    $thumb2_crop_y = ceil(($thumb_height - $thumb_width) / 2);
} else { // square
    $thumb1_new_width = 60;
    $thumb1_new_height = 60;
    $thumb1_crop_x = 0;
    $thumb1_crop_y = 0;
    $thumb2_new_width = 150;
    $thumb2_new_height = 150;
    $thumb2_crop_x = 0;
    $thumb2_crop_y = 0;
}
$thumb1_tmp_img = imagecreatetruecolor($thumb1_new_width,$thumb1_new_height);
$thumb2_tmp_img = imagecreatetruecolor($thumb2_new_width,$thumb2_new_height);
imagecopyresampled($thumb1_tmp_img, $thumb_img, 0, 0, $thumb1_crop_x, $thumb1_crop_y, $thumb1_new_width, $thumb1_new_height, $thumb_width, $thumb_height);
imagecopyresampled($thumb2_tmp_img, $thumb_img, 0, 0, $thumb2_crop_x, $thumb2_crop_y, $thumb2_new_width, $thumb2_new_height, $thumb_width, $thumb_height);

【问题讨论】:

    标签: php thumbnails crop resize-crop


    【解决方案1】:

    我认为你的 thumb1_tmp_img 接近尾声时出错了。我把它变成了一个类,所以我不必像你一样复制太多,但你可以看到变化。看看这是否适合你:

    class ImageFactory
        {
            public  function MakeThumb($thumb_target = '', $width = 60,$height = 60,$SetFileName = false, $quality = 80)
                {
                    $thumb_img  =   imagecreatefromjpeg($thumb_target);
    
                    // size from
                    list($w, $h) = getimagesize($thumb_target);
    
                    if($w > $h) {
                            $new_height =   $height;
                            $new_width  =   floor($w * ($new_height / $h));
                            $crop_x     =   ceil(($w - $h) / 2);
                            $crop_y     =   0;
                    }
                    else {
                            $new_width  =   $width;
                            $new_height =   floor( $h * ( $new_width / $w ));
                            $crop_x     =   0;
                            $crop_y     =   ceil(($h - $w) / 2);
                    }
    
                    // I think this is where you are mainly going wrong
                    $tmp_img = imagecreatetruecolor($width,$height);
    
                    imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);
    
                    if($SetFileName == false) {
                            header('Content-Type: image/jpeg');
                            imagejpeg($tmp_img);
                    }
                    else
                        imagejpeg($tmp_img,$SetFileName,$quality);
    
                    imagedestroy($tmp_img);
                }
        }
    
    // Initiate class
    $ImageMaker =   new ImageFactory();
    
    // Here is just a test landscape sized image
    $thumb_target   =   'http://media1.santabanta.com/full6/Outdoors/Landscapes/landscapes-246a.jpg';
    
    // 60px x 60px
    $ImageMaker->MakeThumb($thumb_target,60,60,'image60px.jpg');
    
    // Here is a portrait for testing
    $thumb_target   =   'http://imgc.allpostersimages.com/images/P-488-488-90/55/5543/L6HLG00Z/posters/warning-you-are-entering-a-fart-zone.jpg';
    
    // 150px x 150px
    $ImageMaker->MakeThumb($thumb_target,150,150,'image150px.jpg');
    

    【讨论】:

    • 非常感谢。你是对的,这是我出错的 imagecreatetruecolor,我输入了错误的宽度/高度。:)
    • 没问题!我把它作为一个类,这样你就可以看到你不必复制所有代码来制作一个 150px 的版本(或任何其他大小的版本)。您可以在要使用拇指制作器的任何地方包含该类,并使用$ImageMaker-&gt;MakeThumb($thumb_target,60,60,'image60px.jpg'); 生成任意数量的类。无论如何,很高兴它成功了!
    猜你喜欢
    • 2016-05-21
    • 2011-06-25
    • 2011-03-18
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2012-04-23
    • 2019-05-08
    相关资源
    最近更新 更多