PHP:创建一个裁剪的矩形缩略图。

5

我正在尝试使用@imagecopyresampled调整大小和裁剪图像,保持原始纵横比。

思路如下: 1)我固定缩略图的一个维度(例如300x40) 2)从高度中心开始裁剪

enter image description here enter image description here

我尝试阅读文档和stackoverflow上的许多其他问题,但没有结果。 有人可以帮帮我吗?? 我目前的代码如下:

//$img_height, $img_width [original size of the image]

$thumb_width  = 300;
$thumb_height = 40;
$new_img = @imagecreatetruecolor($thumb_width, $thumb_height);
$middle = floor($img_height/2);

$src_x = 0;
$src_y = $middle-($thumb_width/2);

$src_w = $img_width;
$aspectRatio = $img_width/$thumb_width;

//$src_h = ?????

$imgCopyRes = @imagecopyresampled(
                  $new_img, $src_img,
                  0, 0, 
                  $src_x, $src_y, 
                  $thumb_width, $thumb_height, 
                  $src_w, $src_h);

编辑:

非常感谢 @Joshua Burns,通过阅读你的类和编辑你的代码,我已经找到了解决方案,而不需要包含整个文件。

代码:

$target_width  = 300;
$target_height = 40;
$new_img = @imagecreatetruecolor($target_width, $target_height);

$width_ratio  = $target_width  / $img_width;
$height_ratio = $target_height / $img_height;
if($width_ratio > $height_ratio) {
    $resized_width  = $target_width;
    $resized_height = $img_height * $width_ratio;
} else {
    $resized_height = $target_height;
    $resized_width  = $img_width * $height_ratio;
}
// Drop decimal values
$resized_width  = round($resized_width);
$resized_height = round($resized_height);

// Calculations for centering the image
$offset_width  = round(($target_width  - $resized_width) / 2);
$offset_height = round(($target_height - $resized_height) / 2);

$imgCopyRes = @imagecopyresampled(
                  $new_img, $src_img, 
                  $offset_width, $offset_height, 
                  0, 0, 
                  $resized_width, $resized_height, 
                  $img_width, $img_height);  

2
这是一个有趣的问题...我有一些代码可以精确地做到这一点,让我看看能否为您找到它...它可能不是高效的代码,但它可以完成您所要求的任务。 - Joshua Burns
$src_h = $img_height * $aspectRatio; 或者 $src_h = $thumb_height * $aspectRatio; - i--
3个回答

2

好的,这可能对你的需求有些臃肿,但它可以完成工作并且做得很好。

首先,在您的PHP代码中包含或粘贴此类:http://pastebin.com/dnmiUVmk

然后,使用类似以下的方式使用类:

<?php
// Replace 'picture' w/ whatever the name of the file upload.
// Alternately, specify an absolute path to an image already on the server.
$upload_image_tmp_filename = $_FILES['picture']['tmp_name'];
$saveas_image_filename = 'my_resized_image.png';
$max_width = 300;
$max_height = 40;

// If there was a problem, an exception is thrown.
try {
    // Load the image
    $picture = New Image($upload_image_tmp_filename);
    // Save the image, resized.
    $picture->saveFile($saveas_image_filename, $max_width, $max_height, True);
} catch(Exception $e) {
    print $e->getMessage();
}

0
使用Imagick(http://www.php.net/manual/en/class.imagick.php
//instantiate the image magick class
$image = new Imagick($image_path);

//crop and resize the image
$image->cropThumbnailImage(100,100);

// save
$image->writeImage($your_file);

0
你可以使用以下代码计算裁剪参数:
$target_width = 300;
$target_height = 40;
$test_case = array(
    array(233,350),
    array(350,233),
    array(300,40)
);
foreach ($test_case as $test) {
    list($source_width, $source_height) = $test;
    $source_ar = $source_width / $source_height;
    $target_ar = $target_width / $target_height;
    if ($source_ar > $target_ar) {
        $temp_height = $target_height;
        $temp_width = (int) ($target_height * $source_ar);
    } else {
        $temp_width = $target_width;
        $temp_height = (int) ($target_width / $source_ar);
    }
    $temp_crop_hori = (int) (($temp_width - $target_width) / 2);
    $temp_crop_vert = (int) (($temp_height - $target_height) / 2);
    echo "==================
source image: ${source_width}x${source_height}
temp image:   ${temp_width}x${temp_height}
target image: crop ${target_width}x${target_height} from ${temp_crop_hori}, ${temp_crop_vert}
";
}

它打印:

==================
source image: 233x350
temp image:   300x450
target image: crop 300x40 from 0, 205
==================
source image: 350x233
temp image:   300x199
target image: crop 300x40 from 0, 79
==================
source image: 300x40
temp image:   300x40
target image: crop 300x40 from 0, 0

以下是您可以使用的信息:

  1. imagecopyresampled 用于将 233x350 图像调整为 300x450 图像
  2. imagecopy 用于从 300x450 的起始位置0, 205复制一个300x40的部分

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接