Answer the question
In order to leave comments, you need to log in
How to crop an image in PHP?
Imagine that a user uploads some kind of image to the site, and I need to crop this image to a 4:3 ratio. How to do it? Or let's let's give the image its own height (eg 765 and 465).
Answer the question
In order to leave comments, you need to log in
Here is a great handy thing for such things
https://github.com/Intervention/image
The only thing that it does not normally do (although there is such an option) is that it does not position the text normally on pictures.
For this, this is great https://github.com/stil/gd-text
Here, together with them, you can already do anything with minimal effort.
function createImage($filename,$w,$h,$endName){
$name=$filename;
$createImage = imagecreatetruecolor($w, $h);
$transparent = imagecolorallocatealpha($createImage, 0, 0, 0, 127);
imagefill($createImage, 0, 0, $transparent);
imagesavealpha($createImage, true);
$image = imagecreatefrompng($name);
imagecopyresampled($createImage, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
$endImage=imagepng($createImage, $endName, 9);
return $endImage;
}
createImage('входное изображение',высота, ширина,'изображение на выходе');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question