画像をアップロードするときにファイル名を変更したい場合は、次のクラスが役立ちます。
<?php
function thumbnail( $img, $source, $dest, $maxw, $maxh ) {
$jpg = $source.$img;
if( $jpg ) {
list( $width, $height ) = getimagesize( $jpg ); //$type will return the type of the image
$source = imagecreatefromjpeg( $jpg );
if( $maxw >= $width && $maxh >= $height ) {
$ratio = 1;
}elseif( $width > $height ) {
$ratio = $maxw / $width;
}else {
$ratio = $maxh / $height;
}
$thumb_width = round( $width * $ratio ); //get the smaller value from cal # floor()
$thumb_height = round( $height * $ratio );
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height );
$path = $dest.$img."-300x200.jpg";
imagejpeg( $thumb, $path, 75 );
}
imagedestroy( $thumb );
imagedestroy( $source );
}
?>
どこ
$img => image file name
$source => the path to the source image
$dest => the path to the destination image
$maxw => the maximum of the image width you desire
$maxh => the minimum one