Я пытаюсь обрезать изображение на PHP
. Я видел, что есть функция для этого на PHP
: imagecrop. Вот код, в котором я пытаюсь обрезать изображение.
$image = imagecreatefromjpeg(//Path of the image);
$croppedImage = imagecrop($image, array("x"=>0,"y"=>0,"width"=>100,"height"=>100));
imagejpeg($croppedImage, //Path in which the image will be stored);
Здесь я хочу, чтобы обрезка изображения начиналась в левом углу изображения и получала значения ширины и высоты, которые я поставил выше.
Но это просто изменяет размер моего изображения, а не обрезает его. Что я делаю не так?
РЕДАКТИРОВАТЬ:
Я также попробовал функцию imagecopyresampled. Вот что я пробовал:
dst_image(Destination image link resource) = newImage; //Here the new image that I want to create.
src_image(Source image link resource) = image; //Here the original image.
//Here 0,0 because I want that the new image crop starts in the left corner of the original image.
dst_x(x-coordinate of destination point) = 0;
dst_y(y-coordinate of destination point) = 0;
//Here 0,0 because I want that the crop starts on the 0,0 of the original image
src_x(x-coordinate of source point) = 0;
src_y(y-coordinate of source point) = 0;
dst_w(Destination width) = 150; //The new width of the crop image.
dst_h(Destination height) = 150; //The new height of the crop image.
src_w(Source width) = 500; //The original width of the image.
src_h(Source height) = 500; //The original height of the image.
Таким образом, наконец, функция будет выглядеть так:
$b = imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w , $dst_h , $src_w , $src_h );
У меня просто проблемы в этой функции, поэтому избегайте остальных (save и imagetruecolor и остальное...)
Эта функция дает мне ожидаемый результат, но новое изображение черное, почему?
Заранее спасибо!
Наконец, я получил решение своей проблемы. Я должен был поместить то же значение dst_w
и src_w
. То же самое с dst_h
и src_h
. ¡¡И это работает!
Конечным решением является следующее:
$b = imagecopyresampled ($dst_image, $src_image, 0, 0, 0, 0, 150, 150, 150, 150);
попробуй это:
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // $src_x + $dst_w Crop end X position in original image
$src_h = 220; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/cropped_whatever.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');