Cropping an image using imagecopyresampled ()?

I would like to crop the image using imagecreatetruecolor, and it always visits it, leaving black spaces, or the scale is too large. I want the image to be exactly 191px wide and 90px high, so I also need to resize the image as well as the crop, because this ratio needs to be preserved. Well, there are a few project examples:

enter image description here

The size of the script (simplified) is as follows:

$src_img=imagecreatefromjpeg($photoTemp); list($width,$height)=getimagesize($photoTemp); $dst_img=imagecreatetruecolor(191, 90); imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height); 

The array $ newImage ['crop'] includes:

 ['x'] => $_POST['inp-x'] ['y'] => $_POST['inp-x'] ['width'] => $_POST['inp-width'] ['height'] => $_POST['inp-height'] 

But I get:

enter image description here

Does anyone see what I'm doing wrong?

Thanks, Mike.

+4
source share
4 answers

Well, I myself found the problem, the code should be like this:

 imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 
0
source

you can do it too, I did it myself and it works

(x1, y1) => where the crop begins

(x2, y2) => where the tip ends

 $filename = $_GET['imageurl']; $percent = 0.5; list($width, $height) = getimagesize($filename); $new_width = $_GET['x2'] - $_GET['x1']; $new_height = $_GET['y2'] - $_GET['y1']; $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0 , $_GET['x1'] , $_GET['y1'] , $new_width, $new_height, $new_width, $new_height); // Outputs the image header('Content-Type: image/jpeg'); imagejpeg($image_p, null, 100); 
+3
source

Try

 <?php $dst_img = imagecreatetruecolor($newImage['crop']['width'], $newImage['crop']['height']); imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], 0, 0, $width, $height); 
+1
source

There is also an imagecrop function that allows you to pass an array using x , y , width , and height .

0
source

Source: https://habr.com/ru/post/1339374/


All Articles