$maxHeight || $width > $maxWidth) { $srcX = 0; $srcY = 0; $srcWidth = $width; $srcHeight = $height; $dstWidth = $width; $dstHeight = $height; if (!$crop) { // Resize $ratio = NULL; if ($dstWidth > $maxWidth) { $ratio = $dstWidth / $maxWidth; $dstWidth = $maxWidth; $dstHeight =(int)($dstHeight / $ratio); } if ($dstHeight > $maxHeight) { $ratio = $dstHeight / $maxHeight; $dstHeight = $maxHeight; $dstWidth =(int)($dstWidth / $ratio); } } else { // Cut & resize if ($width > $maxWidth) $dstWidth = $maxWidth; if ($height > $maxWidth) $dstHeight = $maxHeight; if ($width <= $maxWidth) { if ($height > $srcHeight) $srcHeight = $maxHeight; } elseif ($height <= $maxHeight) { if ($width > $maxWidth) $srcWidth = $maxWidth; } else { $srcWidth = (int) ($maxWidth *($height / $maxHeight)); $srcHeight = (int) ($maxHeight *($width / $maxWidth)); if ($srcWidth <= $width) $srcHeight = $height; else $srcWidth = $width; } if ($width !== $srcWidth) $srcX = (int) (($width / 2) -($srcWidth / 2)); if ($height !== $srcHeight) $srcY = (int) (($height / 2) -($srcHeight / 2)); } $resizedImage = imagecreatetruecolor($dstWidth, $dstHeight); imagealphablending($resizedImage, FALSE); imagesavealpha($resizedImage, TRUE); imagecopyresampled($resizedImage, $image, 0, 0, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight); $saved = imagepng($resizedImage, $dstFile); imagedestroy($resizedImage); } elseif (isset($symbolicSrc)) { $saved = symlink($symbolicSrc, $dstFile); } else { imagesavealpha($image, TRUE); $saved = imagepng($image, $dstFile); } if (!$saved) throw new UserException(sprintf(s('File save error: %s'), $dstFile)); } }