Хорошо, ниже представлен объект Image, который я использую в своем магазине.Он поддерживает масштаб - требует GD
<?php
class Store_Model_Image extends My_Model_Abstract
{
const PATH = STORE_MODEL_IMAGE_PATH;
const URL = "/store-assets/product-images/";
public function get_image_url($width, $height)
{
$old_file = self::PATH . $this->get_filename();
$basename = pathinfo($old_file, PATHINFO_FILENAME);
$new_name = sprintf("%s_%sx%s.jpg", $basename, $width, $height);
if(file_exists(self::PATH . $new_name))
{
return self::URL . $new_name;
}
else
{
list($width_orig, $height_orig, $image_type) = @getimagesize($old_file);
$img = FALSE;
// Get the image and create a thumbnail
switch($image_type)
{
case 1:
$img = @imagecreatefromgif($old_file);
break;
case 2:
$img = @imagecreatefromjpeg($old_file);
break;
case 3:
$img = @imagecreatefrompng($old_file);
break;
}
if(!$img)
{
throw new Zend_Exception("ERROR: Could not create image handle from path.");
}
// Build the thumbnail
if($width_orig > $height_orig)
{
$width_ratio = $width / $width_orig;
$new_width = $width;
$new_height = $height_orig * $width_ratio;
}
else
{
$height_ratio = $height / $height_orig;
$new_width = $width_orig * $height_ratio;
$new_height = $height;
}
$new_img = @imagecreatetruecolor($new_width, $new_height);
// Fill the image black
if(!@imagefilledrectangle($new_img, 0, 0, $new_width, $new_height, 0))
{
throw new Zend_Exception("ERROR: Could not fill new image");
}
if(!@imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig))
{
throw new Zend_Exception("ERROR: Could not resize old image onto new bg.");
}
// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img, NULL, 100);
$image_contents = ob_get_contents();
ob_end_clean();
// lastly (for the example) we are writing the string to a file
$fh = fopen(self::PATH . $new_name, "a+");
fwrite($fh, $image_contents);
fclose($fh);
return self::URL . $new_name;
}
}
}
Я изменяю размер изображения во время запроса, поэтому при первой загрузке страницы изображение будет изменено до требуемого размера для шаблона.(это означает, что мне не нужно аварийно завершать работу общего хоста, пытающегося восстанавливать миниатюры изображений при каждом изменении моего дизайна)
Таким образом, в шаблоне вы передаете объект изображения, а когда вам нужен большой палец изображения,
<img src="<?php echo $image->get_image_url(100, 100); ?>" />
теперь у вас есть большой палец 100x100, который сохраняется на сервере для повторного использования в более поздний срок