たとえば、定義されたトリミング画像のクラスを使用して、codeigniterのライブラリまたはヘルパーを介してクラスと呼ぶことができます。
たとえば、次のように呼びます。
LibraryCropImage::ResizedThumbnail($profile_thumbsize_x, $profile_thumbsize_x, $path, $filename, $path_to, $filename_to.'_sized', $quality, $fileextension);
LibraryCropImage::CroppedThumbnail($profile_thumbsize_x, $profile_thumbsize_x, $path, $filename, $path_to, $filename_to.'_cropped', $quality, $fileextension);
abstract class LibraryCropImage {
function ResizedThumbnail($thumbnail_width, $thumbnail_height, $path, $filename, $path_to, $filename_to, $quality, $ext)
{
list($width, $height) = getimagesize($path.$filename);
$new_width = $thumbnail_width;
$new_height = $thumbnail_height;
if ($ext == "jpg" || $ext == "jpeg")
{
$thumb = @imagecreatefromjpeg($path.$filename);
}
else if ($ext == "gif")
{
$thumb = @imagecreatefromgif($path.$filename);
}
else if ($ext == "png")
{
$thumb = @imagecreatefrompng($path.$filename);
}
$thumbp = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($thumbp, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
ob_start();
if ($ext == "jpg" || $ext == "jpeg")
{
imagejpeg($thumbp, null, $quality);
}
else if ($ext == "gif")
{
imagegif($thumbp);
}
else if ($ext == "png")
{
imagepng($thumbp, null);
}
$i = ob_get_clean();
$fp = fopen($path_to.$filename_to.'.'.$ext, 'w');
fwrite ($fp, $i);
fclose ($fp);
imagedestroy($thumbp);
}
function CroppedThumbnail($thumbnail_width, $thumbnail_height, $path, $filename, $path_to, $filename_to, $quality, $ext)
{
list($width, $height) = getimagesize($path.$filename);
$new_width = $thumbnail_width;
$new_height = $thumbnail_height;
if ($ext == "jpg" || $ext == "jpeg")
{
$image = @imagecreatefromjpeg($path.$filename);
}
else if ($ext == "gif")
{
$image = @imagecreatefromgif($path.$filename);
}
else if ($ext == "png")
{
$image = @imagecreatefrompng($path.$filename);
}
$filename = $path.$filename;
$thumb_width = $thumbnail_width;
$thumb_height = $thumbnail_height;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if($original_aspect >= $thumb_aspect) {
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
} else {
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
ob_start();
if ($ext == "jpg" || $ext == "jpeg")
{
imagejpeg($thumb, null, $quality);
}
else if ($ext == "gif")
{
imagegif($thumb);
}
else if ($ext == "png")
{
imagepng($thumb, null);
}
$i = ob_get_clean();
$fp = fopen($path_to.$filename_to.'.'.$ext, 'w');
fwrite ($fp, $i);
fclose ($fp);
imagedestroy($thumb);
}
}