$thumbWidth || $srcHeight > $thumbHeight) { if ($thumbWidth == 0) { $thumbWidth = $thumbHeight * $srcWidth / $srcHeight; } elseif ($thumbHeight == 0) { $thumbHeight = $thumbWidth * $srcHeight / $srcWidth; } else { $a = $thumbWidth / $thumbHeight; $b = $srcWidth / $srcHeight; if ($a > $b) { $thumbWidth = $b * $thumbHeight; } else { $thumbHeight = $thumbWidth / $b; } } $thumbWidth = (int) round($thumbWidth); $thumbHeight = (int) round($thumbHeight); $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight); // preserve png transparency imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $srcWidth, $srcHeight ); imagedestroy($source); $source = $thumb; } return $source; } /** * Creates an image from a string preserving PNG transparency. * * @param $imageString * * @return resource */ function imageFromString($imageString) { $image = imagecreatefromstring($imageString); // preserve PNG transparency imagealphablending($image, false); imagesavealpha($image, true); return $image; }