Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please forgive me if there are any typos or anything in the following code. I haven't tested it. What I've done here is calculate whether the height or the width is the proportion that's too long. Then adjust the source dimension to match the final image dimension. Also, adjust the center of the side that we shrunk so the cropped image is centered.</p> <pre><code> $newwidth = 500; $newheight = 375; $tmp = imagecreatetruecolor($newwidth, $newheight); $widthProportion = $width / $newwidth; $heightProportion = $height / $newheight; if ($widthProportion &gt; $heightProportion) { // width proportion is greater than height proportion // figure out adjustment we need to make to width $widthAdjustment = ($width * ($widthProportion - $heightProportion)); // Shrink width to proper proportion $width = $width - $widthAdjustment; $x = 0; // No adjusting height position $y = $y + ($widthAdjustment / 2); // Center the adjustment } else { // height proportion is greater than width proportion // figure out adjustment we need to make to width $heightAdjustment = ($height * ($heightProportion - $widthProportion)); // Shrink height to proper proportion $height = $height - $heightAdjustment; $x = $x + ($heightAdjustment / 2); // Center the ajustment $y = 0; // No adjusting width position } imagecopyresampled($tmp, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height); </code></pre> <p>So basically with the $width and $height variables you are specifying how much of the picture you want (cropping). and with the $x, $y we're saying where we want to crop the picture. The rest is just the standard resizing to fit the full new image.</p> <p>I hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload