function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height, $force_resize = false, $preferred_format = 0) { global $gd2, $modSettings; // Without GD, no image resizing at all. if (!checkGD()) { return false; } $success = false; // Determine whether to resize to max width or to max height (depending on the limits.) if (!empty($max_width) || !empty($max_height)) { if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height)) { $dst_width = $max_width; $dst_height = floor($src_height * $max_width / $src_width); } elseif (!empty($max_height)) { $dst_width = floor($src_width * $max_height / $src_height); $dst_height = $max_height; } // Don't bother resizing if it's already smaller... if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize)) { // (make a true color image, because it just looks better for resizing.) if ($gd2) { $dst_img = imagecreatetruecolor($dst_width, $dst_height); // Deal nicely with a PNG - because we can. if (!empty($preferred_format) && $preferred_format == 3) { imagealphablending($dst_img, false); if (function_exists('imagesavealpha')) { imagesavealpha($dst_img, true); } } } else { $dst_img = imagecreate($dst_width, $dst_height); } // Resize it! if ($gd2) { imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } else { imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } } else { $dst_img = $src_img; } } else { $dst_img = $src_img; } // Save the image as ... if (!empty($preferred_format) && $preferred_format == 3 && function_exists('imagepng')) { $success = imagepng($dst_img, $destName); } elseif (!empty($preferred_format) && $preferred_format == 1 && function_exists('imagegif')) { $success = imagegif($dst_img, $destName); } elseif (function_exists('imagejpeg')) { $success = imagejpeg($dst_img, $destName); } // Free the memory. imagedestroy($src_img); if ($dst_img != $src_img) { imagedestroy($dst_img); } return $success; }
/** * Resizes an image proportionally to fit within the defined max_width and max_height limits * * - Will do nothing to the image if the file fits within the size limits * - If Image Magick is present it will use those function over any GD solutions * - If GD2 is present, it'll use it to achieve better quality (imagecopyresampled) * - Saves the new image to destination_filename, in the preferred_format * if possible, default is jpeg. * * @uses GD * @uses Imagick * * @package Graphics * @param resource|null $src_img null for Imagick images, resource form imagecreatefrom for GD * @param string $destName * @param int $src_width * @param int $src_height * @param int $max_width * @param int $max_height * @param bool $force_resize = false * @param int $preferred_format = 0 */ function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height, $force_resize = false, $preferred_format = 0) { global $gd2; if (checkImagick()) { // These are the file formats we know about static $default_formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png', '6' => 'bmp', '15' => 'wbmp'); $preferred_format = empty($preferred_format) || !isset($default_formats[$preferred_format]) ? 2 : $preferred_format; // Since Imagick can throw exceptions, lets catch them try { // Get a new instance of Imagick for use $imagick = new Imagick($destName); // Set the input and output image size $src_width = empty($src_width) ? $imagick->getImageWidth() : $src_width; $src_height = empty($src_height) ? $imagick->getImageHeight() : $src_height; $dest_width = empty($max_width) ? $src_width : $max_width; $dest_height = empty($max_height) ? $src_height : $max_height; // Create a new image in our prefered format and resize it if needed $imagick->setImageFormat($default_formats[$preferred_format]); $imagick->resizeImage($dest_width, $dest_height, Imagick::FILTER_LANCZOS, 1, true); // Save the new image in the destination location $success = $imagick->writeImage($destName); // Free resources associated with the Imagick object $imagick->destroy(); } catch (Exception $e) { // Not currently used, but here is the error $success = $e->getMessage(); $success = false; } return !empty($success); } elseif (checkGD()) { $success = false; // Determine whether to resize to max width or to max height (depending on the limits.) if (!empty($max_width) || !empty($max_height)) { if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height)) { $dst_width = $max_width; $dst_height = floor($src_height * $max_width / $src_width); } elseif (!empty($max_height)) { $dst_width = floor($src_width * $max_height / $src_height); $dst_height = $max_height; } // Don't bother resizing if it's already smaller... if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize)) { // (make a true color image, because it just looks better for resizing.) if ($gd2) { $dst_img = imagecreatetruecolor($dst_width, $dst_height); // Deal nicely with a PNG - because we can. if (!empty($preferred_format) && $preferred_format == 3) { imagealphablending($dst_img, false); if (function_exists('imagesavealpha')) { imagesavealpha($dst_img, true); } } } else { $dst_img = imagecreate($dst_width, $dst_height); } // Resize it! if ($gd2) { imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } else { imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } } else { $dst_img = $src_img; } } else { $dst_img = $src_img; } // Save the image as ... if (!empty($preferred_format) && $preferred_format == 3 && function_exists('imagepng')) { $success = imagepng($dst_img, $destName); } elseif (!empty($preferred_format) && $preferred_format == 1 && function_exists('imagegif')) { $success = imagegif($dst_img, $destName); } elseif (function_exists('imagejpeg')) { $success = imagejpeg($dst_img, $destName, 80); } // Free the memory. imagedestroy($src_img); if ($dst_img != $src_img) { imagedestroy($dst_img); } return $success; } else { return false; } }
function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height) { global $gd2, $modSettings; // Determine whether to resize to max width or to max height (depending on the limits.) if (!empty($max_width) || !empty($max_height)) { if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height)) { $dst_width = $max_width; $dst_height = floor($src_height * $max_width / $src_width); } elseif (!empty($max_height)) { $dst_width = floor($src_width * $max_height / $src_height); $dst_height = $max_height; } // Don't bother resizing if it's already smaller... if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height)) { // (make a true color image, because it just looks better for resizing.) if ($gd2) { $dst_img = imagecreatetruecolor($dst_width, $dst_height); if (!empty($modSettings['avatar_download_png'])) { imagealphablending($dst_img, false); if (function_exists('imagesavealpha')) { imagesavealpha($dst_img, true); } } } else { $dst_img = imagecreate($dst_width, $dst_height); } // Resize it! if ($gd2) { imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } else { imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } } else { $dst_img = $src_img; } } else { $dst_img = $src_img; } // Save it! if (!empty($modSettings['avatar_download_png'])) { imagepng($dst_img, $destName); } else { imagejpeg($dst_img, $destName, 65); } // Free the memory. imagedestroy($src_img); if ($dst_img != $src_img) { imagedestroy($dst_img); } }
function save_thumbnail($image, $size, $save_path, $save_name, $config = array()) { $myreturn = false; $size = array($size, $size); if (empty($config['padding_type'])) { $config['padding_type'] = PAD_1SIDE; } if (empty($config['quality'])) { $config['quality'] = 90; } if ($imginfo = getimagesize($image)) { $orig_size = array($imginfo[0], $imginfo[1]); if ($orig_size[0] / $size[0] < $orig_size[1] / $size[1]) { $relevant_length = 1; } else { $relevant_length = 0; } if ($imginfo[2] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) { //gif $myimg = @imagecreatefromgif($image); } elseif ($imginfo[2] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) { //jpg ob_start(); $myimg = @imagecreatefromjpeg($image); ob_end_flush(); } elseif ($imginfo[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')) { //png $myimg = @imagecreatefrompng($image); } if (!empty($myimg)) { $new_size = array(); $mynewimg = ''; if ($orig_size[$relevant_length] > $size[$relevant_length]) { // scale down $new_size[$relevant_length] = $size[$relevant_length]; $new_size[1 - $relevant_length] = (int) ($orig_size[1 - $relevant_length] * ($size[$relevant_length] / $orig_size[$relevant_length])); if ($config['padding_type'] == PAD_1SIDE || $config['padding_type'] == PAD_2SIDES) { // $size=$size; // this is actually just PAD_1SIDE and the photo will be square } else { $size = $new_size; // no padding here, photo has original proportions } } else { // picture is smaller than the needed size $new_size = $orig_size; if ($config['padding_type'] == PAD_2SIDES) { //pad in both directions. square and big // $size=array($size,$size); } elseif ($config['padding_type'] == PAD_1SIDE) { // padding in one direction only. square but smaller $size = $orig_size[$relevant_length]; $size = array($size, $size); } else { // no padding. original proportions $size = $orig_size; } } $mynewimg = @imagecreatetruecolor($size[0], $size[1]); imagefilledrectangle($mynewimg, 0, 0, $size[0], $size[1], 0xffffff); $x = (int) (($size[0] - $new_size[0]) / 2); $y = (int) (($size[1] - $new_size[1]) / 2); if (defined('BICUBIC_RESAMPLE')) { imagecopyresamplebicubic($mynewimg, $myimg, $x, $y, 0, 0, $new_size[0], $new_size[1], $orig_size[0], $orig_size[1]); } else { fastimagecopyresampled($mynewimg, $myimg, $x, $y, 0, 0, $new_size[0], $new_size[1], $orig_size[0], $orig_size[1]); } if (!empty($config['watermark_text']) && function_exists('imagettftext')) { $config['watermark_text_color'] = str_pad($config['watermark_text_color'], 6, '0', STR_PAD_RIGHT); $text_color = imagecolorallocate($mynewimg, hexdec(substr($config['watermark_text_color'], 0, 2)), hexdec(substr($config['watermark_text_color'], 2, 2)), hexdec(substr($config['watermark_text_color'], 4, 2))); $text_color2 = imagecolorallocate($mynewimg, 255 - hexdec(substr($config['watermark_text_color'], 0, 2)), 255 - hexdec(substr($config['watermark_text_color'], 2, 2)), 255 - hexdec(substr($config['watermark_text_color'], 4, 2))); $font_size = 15; do { --$font_size; $text_box = imagettfbbox($font_size, 0, _BASEPATH_ . '/includes/fonts/arial.ttf', $config['watermark_text']); $textlen = $text_box[2] - $text_box[0] + 5; } while ($textlen > $new_size[0]); $watermark_x = (int) (($size[0] - $new_size[0]) / 2) + 5; $watermark_y = $new_size[1] + (int) (($size[1] - $new_size[1]) / 2) - 20; //shadow first imagettftext($mynewimg, $font_size, 0, $watermark_x, $watermark_y, $text_color2, _BASEPATH_ . '/includes/fonts/arial.ttf', $config['watermark_text']); //text second imagettftext($mynewimg, $font_size, 0, $watermark_x + 1, $watermark_y + 1, $text_color, _BASEPATH_ . '/includes/fonts/arial.ttf', $config['watermark_text']); } if (!empty($config['watermark_image']) && is_file($config['watermark_image'])) { $wm_image = @imagecreatefrompng($config['watermark_image']); $wm_image_width = imagesx($wm_image); $wm_image_height = imagesy($wm_image); $wm_image_x = (int) (($size[0] - $new_size[0]) / 2) + 5; $wm_image_y = $new_size[1] + (int) (($size[1] - $new_size[1]) / 2) - $wm_image_height; if (defined('BICUBIC_RESAMPLE')) { imagecopyresamplebicubic($mynewimg, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_width, $wm_image_height, $wm_image_width, $wm_image_height); } else { fastimagecopyresampled($mynewimg, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_width, $wm_image_height, $wm_image_width, $wm_image_height); } } if (!empty($config['round_corners'])) { $skin = get_my_skin(); imagealphablending($mynewimg, true); // put the corners $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_tl.png'); imagecopy($mynewimg, $corner, 0, 0, 0, 0, 7, 7); $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_tr.png'); imagecopy($mynewimg, $corner, $size[0] - 7, 0, 0, 0, 7, 7); $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_bl.png'); imagecopy($mynewimg, $corner, 0, $size[1] - 7, 0, 0, 7, 7); $corner = @imagecreatefrompng(_BASEPATH_ . '/skins_site/' . $skin . '/images/corner_br.png'); imagecopy($mynewimg, $corner, $size[0] - 7, $size[1] - 7, 0, 0, 7, 7); // draw the border lines $border_color = imagecolorallocate($mynewimg, 0xcc, 0xcc, 0xcc); imageline($mynewimg, 7, 0, $size[0] - 8, 0, $border_color); //tl->tr imageline($mynewimg, $size[0] - 1, 7, $size[0] - 1, $size[1] - 8, $border_color); //tr->br imageline($mynewimg, 7, $size[1] - 1, $size[0] - 8, $size[1] - 1, $border_color); //bl->br imageline($mynewimg, 0, 7, 0, $size[1] - 8, $border_color); //tl->bl } $myreturn = imagejpeg($mynewimg, $save_path . '/' . $save_name . '.jpg', $config['quality']); } else { $myreturn = false; } } else { $myreturn = false; } if (!empty($myimg)) { imagedestroy($myimg); } if (!empty($mynewimg)) { imagedestroy($mynewimg); } return $myreturn; }
/** * Resizes src_img proportionally to fit within max_width and max_height limits * if it is too large. * If GD2 is present, it'll use it to achieve better quality. * It saves the new image to destination_filename, as preferred_format * if possible, default is jpeg. * @uses GD * * @param resource $src_img * @param string $destName * @param int $src_width * @param int $src_height * @param int $max_width * @param int $max_height * @param bool $force_resize = false * @param int $preferred_format = 0 */ function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height, $force_resize = false, $preferred_format = 0) { global $gd2, $modSettings; if (checkImagick()) { static $default_formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png', '6' => 'bmp', '15' => 'wbmp'); $preferred_format = empty($preferred_format) || !isset($default_formats[$preferred_format]) ? 2 : $preferred_format; $imagick = new Imagick($destName); $src_width = empty($src_width) ? $imagick->getImageWidth() : $src_width; $src_height = empty($src_height) ? $imagick->getImageHeight() : $src_height; $dest_width = empty($max_width) ? $src_width : $max_width; $dest_height = empty($max_height) ? $src_height : $max_height; $imagick->setImageFormat($default_formats[$preferred_format]); $imagick->resizeImage($dest_width, $dest_height, Imagick::FILTER_LANCZOS, 1, true); $success = $imagick->writeImage($destName); return !empty($success); } elseif (checkGD()) { $success = false; // Determine whether to resize to max width or to max height (depending on the limits.) if (!empty($max_width) || !empty($max_height)) { if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height)) { $dst_width = $max_width; $dst_height = floor($src_height * $max_width / $src_width); } elseif (!empty($max_height)) { $dst_width = floor($src_width * $max_height / $src_height); $dst_height = $max_height; } // Don't bother resizing if it's already smaller... if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize)) { // (make a true color image, because it just looks better for resizing.) if ($gd2) { $dst_img = imagecreatetruecolor($dst_width, $dst_height); // Deal nicely with a PNG - because we can. if (!empty($preferred_format) && $preferred_format == 3) { imagealphablending($dst_img, false); if (function_exists('imagesavealpha')) { imagesavealpha($dst_img, true); } } } else { $dst_img = imagecreate($dst_width, $dst_height); } // Resize it! if ($gd2) { imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } else { imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); } } else { $dst_img = $src_img; } } else { $dst_img = $src_img; } // Save the image as ... if (!empty($preferred_format) && $preferred_format == 3 && function_exists('imagepng')) { $success = imagepng($dst_img, $destName); } elseif (!empty($preferred_format) && $preferred_format == 1 && function_exists('imagegif')) { $success = imagegif($dst_img, $destName); } elseif (function_exists('imagejpeg')) { $success = imagejpeg($dst_img, $destName); } // Free the memory. imagedestroy($src_img); if ($dst_img != $src_img) { imagedestroy($dst_img); } return $success; } else { // Without GD, no image resizing at all. return false; } }