PHP: создайте обрезанный эскиз прямоугольника
Я пытаюсь изменить размер и обрезать изображение с @imagecopyresampled
сохранением исходного соотношения сторон.
Идея заключается в том,: 1) Я фиксирую размер миниатюры (например, 300x40) 2) Обрезать, начиная с центра высоты
Я попытался прочитать документацию и множество других вопросов по 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 Бернс, читая ваш класс и редактируя ваш код, я нашел решение, не включая весь файл.
Код:
$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);
3 answers
Хорошо, это, вероятно, немного раздуто для ваших нужд, но это делает работу, и делает ее хорошо..
Во-первых, включите или вставьте этот класс в свой 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();
}
Используйте 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);
Для расчета параметров обрезки можно использовать следующий код:
$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
Эту информацию вы можете использовать в:
-
imagecopyresampled
для изменения размера 233x350 изображения в 300x450 изображения -
imagecopy
для копирования части 300x40 из 300x450, начиная с 0, 205