H
H
hellcaster2018-04-25 17:26:25
PHP
hellcaster, 2018-04-25 17:26:25

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

2 answer(s)
A
Alexander Aksentiev, 2018-04-25
@Sanasol

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.

R
Roma, 2018-04-25
@B_Roma

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('входное изображение',высота, ширина,'изображение на выходе');

used this script for png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question