Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You will have to modify your resizing code not to be based on the cropped image to start with. Since you want to do the cropping and resizing in one go you need to calculate it independently.</p> <pre><code>&lt;?php function Image($image, $crop = ':', $size = null) { $image = ImageCreateFromString(file_get_contents($image)); if (is_resource($image)) { $x = 0; $y = 0; $width = imagesx($image); $height = imagesy($image); // CROP (Aspect Ratio) Section $crop = array_filter(explode(':', $crop)); if (empty($crop)) { $crop = [$width, $height]; } else { $crop[0] = $crop[0] ?: $crop[1]; $crop[1] = $crop[1] ?: $crop[0]; } $ratio = [$width / $height, $crop[0] / $crop[1]]; if ($ratio[0] &gt; $ratio[1]) { $width = $height * $ratio[1]; $x = (imagesx($image) - $width) / 2; } else { $height = $width / $ratio[1]; $y = (imagesy($image) - $height) / 2; } // Resize Section if (is_null($size)) { $size = [$width, $height]; } else { $size = array_filter(explode('x', $size)); if (empty($size)) { $size = [imagesx($image), imagesy($image)]; } else { $size[0] = $size[0] ?: round($size[1] * $width / $height); $size[1] = $size[1] ?: round($size[0] * $height / $width); } } $result = ImageCreateTrueColor($size[0], $size[1]); if (is_resource($result)) { ImageSaveAlpha($result, true); ImageAlphaBlending($result, true); ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255)); ImageCopyResampled($result, $image, 0, 0, $x, $y, $size[0], $size[1], $width, $height); ImageInterlace($result, true); ImageJPEG($result, null, 90); } } return false; } header('Content-Type: image/jpeg'); Image('http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png', '1:1', '600x'); ?&gt; </code></pre>
 

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