Melanholly
Registered
Намерих го в php.net и би трябвало да създава thumbnail на големи изображения, но неработи както трябва. Скрипта е този:
А ето как аз използвам функцията
Весели празници.
Код:
function create_thumbnail( $source_file, $destination_file, $max_dimension)
{
list($img_width,$img_height) = getimagesize($source_file); // Get the original dimentions
$aspect_ratio = $img_width / $img_height;
if ( ($img_width > $max_dimension) || ($img_height > $max_dimension) ) // If either dimension is too big...
{
if ( $img_width > $img_height ) // For wide images...
{
$new_width = $max_dimension;
$new_height = $new_width / $aspect_ratio;
}
elseif ( $img_width < $img_height ) // For tall images...
{
$new_height = $max_dimension;
$new_width = $new_height * $aspect_ratio;
}
elseif ( $img_width == $img_height ) // For square images...
{
$new_width = $max_dimension;
$new_height = $max_dimension;
}
else { echo "Error reading image size."; return FALSE; }
}
else { $new_width = $img_width; $new_height = $img_height; } // If it's already smaller, don't change the size.
// Make sure these are integers.
$new_width = intval($new_width);
$new_height = intval($new_height);
$thumbnail = imagecreatetruecolor($new_width,$new_height); // Creates a new image in memory.
// The following block retrieves the source file. It assumes the filename extensions match the file's format.
if ( strpos($source_file,".gif") ) { $img_source = imagecreatefromgif($source_file); }
if ( (strpos($source_file,".jpg")) || (strpos($source_file,".jpeg")) )
{ $img_source = imagecreatefromjpeg($source_file); }
if ( strpos($source_file,".bmp") ) { $img_source = imagecreatefromwbmp($source_file); }
if ( strpos($source_file,".png") ) { $img_source = imagecreatefrompng($source_file); }
// Here we resample and create the new jpeg.
imagecopyresampled($thumbnail, $img_source, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
imagejpeg( $thumbnail, $destination_file, 100 );
// Finally, we destroy the two images in memory.
imagedestroy($img_source);
imagedestroy($thumbnail);
}
Код:
create_thumbnail("img.jpg","img.jpg",100);