Фuлuн (09.01.2011/13:05)
Вопрос: можно ли копировать картинку из одной папки в другую но что бы картинка была 100x100 а после копирования 32x32
function image_resize( $source_path, //Путь до файлу
$destination_path, //имя и путь нового файла
$newwidth, //Размер изображения
$newheight = FALSE,
$quality = FALSE // качество для формата jpeg
) {
list($oldwidth, $oldheight, $type) = getimagesize($source_path);
switch ($type) {
case 1: $typestr = 'gif' ;break;
case 2: $typestr = 'jpeg'; break;
case 3: $typestr = 'png'; break;
}
if($typestr) {
// Обрабатываем анимацию
if($type == 1){
$images = new Imagick($source_path);
if($images->getNumberImages() > 1){
$images = $images->coalesceImages();
$oldwidth = $images->getImageWidth();
$oldheight = $images->getImageHeight();
if($newheight > $oldheight) {
$newheight = $oldheight;
}
if($newwidth > $oldwidth) {
$newwidth = $oldwidth;
}
if (!$newheight) { $newheight = round($newwidth * $oldheight/$oldwidth); }
elseif (!$newwidth) { $newwidth = round($newheight * $oldwidth/$oldheight); }
do {
$images->scaleImage($newwidth, $newheight);
} while ($images->nextImage());
$images = $images->deconstructImages();
$images->writeImages($destination_path, true);
return;
}
}
$function = "imagecreatefrom$typestr";
$src_resource = $function($source_path);
if($newheight > $oldheight) {
$newheight = $oldheight;
}
if($newwidth > $oldwidth) {
$newwidth = $oldwidth;
}
if (!$newheight) { $newheight = round($newwidth * $oldheight/$oldwidth); }
elseif (!$newwidth) { $newwidth = round($newheight * $oldwidth/$oldheight); }
$destination_resource = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($destination_resource, $src_resource, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
imagegammacorrect($destination_resource, 1, 1.1);
if ($type == 2) { # jpeg
imageinterlace($destination_resource, 1);
if ($quality) imagejpeg($destination_resource, $destination_path, $quality);
else imagejpeg($destination_resource, $destination_path);
} else { # gif, png
$function = "image$typestr";
$function($destination_resource, $destination_path);
}
imagedestroy($destination_resource);
imagedestroy($src_resource);
}
}
для обработки анимации требуется библиотека imagick
если этой библиотеки нет на хосте, то закомментируй соответствующее условие