Script Image Resizing Process (saved as image_process.class.php)
by HR
<?php
class image_process {
var $copyright = "";
var $font_size;
function create_image($source_path,$out_path,$quality=75,$width=0,$height=0,$aspect_ratio=true,$thumbnail=false) {
// get the main image
$source_image = @imagecreatefromjpeg($source_path) ;
if (!$source_image) {
return;
}
$real_width = imagesx($source_image);
$real_height = imagesy($source_image);
$landscape = false;
if ($real_width > $real_height) {
$landscape = true;
}
// scaled size
if ($width == 0) { $scaled_width = $real_width; }
else { $scaled_width = $width; }
if ($height == 0) { $scaled_height = $real_height; }
else { $scaled_width = $width; }
if ($aspect_ratio) {
$scaled_ratio = $scaled_width / $scaled_height;
$image_ratio = $real_width / $real_height;
if ($scaled_ratio < $image_ratio) {
$scaled_height = (int)($scaled_width / $image_ratio);
} else {
$scaled_width = (int)($scaled_height * $image_ratio);
}
}
// create the canvas
if ($thumbnail) {
$image_ratio = $real_width / $real_height;
if ($landscape) {
$small_height = $height;
$small_width = (int)($small_height * $image_ratio);
}
else {
$small_width = $width;
$small_height = (int)($small_width / $image_ratio);
}
if ($landscape) {
$thumb_width = ($small_width-$width) / 2;
$thumb_height = 0;
}
else {
$thumb_height = ($small_height-$height) / 2;
$thumb_width = 0;
}
$dest_image = imagecreatetruecolor($width,$height) ;
$dest_thumb = imagecreatetruecolor($small_width,$small_height) ;
imagecopyresized($dest_thumb,$source_image,0,0,0,0,$small_width,$small_height,$real_width,$real_height) ;
imagecopy($dest_image,$dest_thumb,0,0,$thumb_width,$thumb_height,$width,$height) ;
}
else {
$dest_image = imagecreatetruecolor($scaled_width,$scaled_height) ;
imagecopyresized($dest_image,$source_image,0,0,0,0,$scaled_width,$scaled_height,$real_width,$real_height) or die("Problem In Resizing");
}
$real_width = imagesx($dest_image);
$real_height = imagesy($dest_image);
// copyright
if ($this->copyright != "") {
$char_width = imagefontwidth($this->font_size);
$char_height = imagefontheight($this->font_size);
$padding = 2;
$width = $padding * 2 + strlen($this->copyright) * $char_width;
$height = + $padding * 2 + $char_height;
$background = imagecolorallocate($dest_image, 200, 200, 200);
$text = imagecolorallocate($dest_image, 0, 0, 0);
imagefilledrectangle ($dest_image, 0, 0, $width, $height, $background);
imagestring($dest_image, $this->font_size, $padding, $padding, $this->copyright, $text);
}
imagejpeg($dest_image,$out_path,$quality) ;
imagedestroy($dest_image) ;
}
}
?>