-1

How to resize and crop center of image with high quality image ?

EG; example for vertical image

first i have image 200x300px , i want to resize image width or height to 100 px

then crop (100x100 px) center of image with high quality image

last upload to server .

please advide me for code or script that can do that

thank you ^^

please image in this link ^^

http://image.ohozaa.com/i/1d6/IvHU1b.png

2 Answers2

1

If you are doing it in PHP, use the ImageMagick extension. Just use the Imagick::scaleImage() and Imagick::cropImage() methods.

Note: You can use the Imagick::getSize() method to get the width and height of an image, and then just take the smaller of the two, divide it by 100 to get the scale factor, and divide both the width and height to set as the arguments for the scale method. Then check how much longer than 100 the larger side is than 100, and use half that number for the associated offset in the crop method.

Another option would be to use just use CSS to scale and center the image. See this answer for more on how to do that.

Community
  • 1
  • 1
Ruckus T-Boom
  • 4,566
  • 1
  • 28
  • 42
0

It worked for me. You can try:

// my final thumb
$x = 100;
$y = 100;
// ratio thumb
$ratio_thumb = $x / $y;
// original size
list($xx, $yy) = getimagesize($image);
// ratio original
$ratio_original = $xx / $yy;

if ($ratio_original >= $ratio_thumb) {
    $yo = $yy;
    $xo = ceil(($yo * $x) / $y);
    $xo_ini = ceil(($xx - $xo) / 2);
    $xy_ini = 0;
} else {
    $xo = $xx;
    $yo = ceil(($xo * $y) / $x);
    $xy_ini = ceil(($yy - $yo) / 2);
    $xo_ini = 0;
}

imagecopyresampled($thumb, $source, 0, 0, $xo_ini, $xy_ini, $x, $y, $xo, $yo);
Offboard
  • 27
  • 5