What are the recommended quality settings for creating JPEG thumbnails using imagejpeg function in PHP?
When creating JPEG thumbnails using the imagejpeg function in PHP, it is recommended to use a quality setting between 60 to 80 for a good balance between image quality and file size. Using a lower quality setting will result in smaller file sizes but may reduce image clarity, while using a higher quality setting will result in larger file sizes but better image quality.
// Create a JPEG thumbnail with recommended quality settings
$source_image = 'original.jpg';
$thumbnail_image = 'thumbnail.jpg';
list($width, $height) = getimagesize($source_image);
$thumb_width = 100;
$thumb_height = 100;
$source = imagecreatefromjpeg($source_image);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
imagejpeg($thumb, $thumbnail_image, 75); // Recommended quality setting between 60 to 80
imagedestroy($source);
imagedestroy($thumb);
Keywords
Related Questions
- What are the differences between mb_convert_encoding and iconv functions in PHP for character encoding conversion?
- What are the potential pitfalls of embedding tests within code, as seen in the provided PHP script?
- Are there any best practices for handling file management in PHP applications to ensure efficiency and security?