function doTransformImage($base_img, $img_file, $im_width, $im_height, $ext = 'jpg') { list($base_width, $base_height, $type, $attr) = getimagesize($base_img); $gd_img_base = imagecreatefromjpeg($base_img); $gd_img_dest = imagecreatetruecolor($im_width, $im_height); imagecopyresampled($gd_img_dest, $gd_img_base, 0, 0, 0, 0, $im_width, $im_height, $base_width, $base_height); if ($ext == 'jpg') { //write out the image: if (!imagejpeg($gd_img_dest, $img_file, 90)) { return false; } } else { if ($im_width == 80) { $dcolors = 128; } else { $dcolors = 256; } ImageTrueColorToPalette2($gd_img_dest, false, $dcolors); if (!imagepng($gd_img_dest, $img_file)) { return false; } } imagedestroy($gd_img_base); imagedestroy($gd_img_dest); //success: return true; }
function display_crop_and_frame($original_image, $original_width, $original_height, $param_width, $param_height) { //error_log("display_crop_and_frame"); //-------------------------------------------- // Create the cropped image //-------------------------------------------- $ratio = $original_width / $param_width > $original_height / $param_height ? $original_height / $param_height : $original_width / $param_width; $cropped_width = floor($ratio * $param_width); $cropped_height = floor($ratio * $param_height); $off_x = floor(($original_width - $cropped_width) / 2); $off_y = floor(($original_height - $cropped_height) / 2); $original_width = $cropped_width; $original_height = $cropped_height; $new_width = $param_width; $new_height = $param_height; $scaled_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($scaled_image, $original_image, 0, 0, $off_x, $off_y, $new_width, $new_height, $original_width, $original_height); // JLR // imagetruecolortopalette($scaled_image, true, 255); ImageTrueColorToPalette2($scaled_image, true, 255); //---------------------------------------- // Create the background image //---------------------------------------- $background_image = imagecreatetruecolor($param_width, $param_height); $black = imagecolorallocate($background_image, 0, 0, 0); imagecolortransparent($background_image, $black); // JLR // imagetruecolortopalette($background_image, true, 255); ImageTrueColorToPalette2($background_image, true, 255); //---------------------------------------- // Calculate the offsets //---------------------------------------- $offset_width = floor(($param_width - $new_width) / 2); $offset_height = floor(($param_height - $new_height) / 2); imagecopymerge($background_image, $scaled_image, $offset_width, $offset_height, 0, 0, $new_width, $new_height, 100); //-------------------------------------------- // Display the image //-------------------------------------------- header('Content-type: image/png'); imagepng($scaled_image); }