/** * Display rendered image (send it to browser or to file). * This method is a common implementation to render and output an image. * The method calls the render() method automatically and outputs the * image to the browser or to the file. * * @param mixed $input Destination image, a filename or an image string data or a GD image resource * @param null $output * @param array $options Thumbnail options * <pre> * width int Width of thumbnail * height int Height of thumbnail * percent number Size of thumbnail per size of original image * method int Method of thumbnail creating * halign int Horizontal align * valign int Vertical align * </pre> * * @return bool TRUE on success or FALSE on failure. * @access public */ function output($input, $output = null, $options = array()) { // Load source file and render image $renderImage = Thumbnail::render($input, $options); if (!$renderImage) { user_error('Error rendering image', E_USER_NOTICE); return false; } // Set output image type // By default PNG image $type = isset($options['type']) ? $options['type'] : IMAGETYPE_PNG; // Before output to browsers send appropriate headers if (empty($output)) { $content_type = image_type_to_mime_type($type); if (!headers_sent()) { header('Content-Type: ' . $content_type); } else { user_error('Headers have already been sent. Could not display image.', E_USER_NOTICE); return false; } } // Define outputing function switch ($type) { case IMAGETYPE_PNG: $result = empty($output) ? imagepng($renderImage) : imagepng($renderImage, $output); break; case IMAGETYPE_JPEG: $result = empty($output) ? imagejpeg($renderImage) : imagejpeg($renderImage, $output); break; default: user_error('Image type ' . $content_type . ' not supported by PHP', E_USER_NOTICE); return false; } // Output image (to browser or to file) if (!$result) { user_error('Error output image', E_USER_NOTICE); return false; } // Free a memory from the target image imagedestroy($renderImage); return true; }
public function getThumbnail($path) { $this->ensureAccess($path); $image = new Thumbnail($path); $this->setImageHeaders($path, $image->getType()); $image->downscale(); $image->render(); }
/** * Create image thumb function * * @param int $id Catalogue item ID * @param string $image Image relative path * @param int $width Width of image * @param int $height Height if image * @param string $suffix Additional suffix for image filename * * @return bool|string */ public static function createThumb($id, $image, $width, $height, $suffix = 'min') { $resized_folder = 'resized'; // Check for the existence of source image $abs_source_img = JPATH_BASE . DS . $image; if (!file_exists($abs_source_img)) { return false; } $images_dir = dirname($image); $abs_images_dir = JPATH_BASE . DS . $images_dir; $abs_new_img_dir = $abs_images_dir . DS . $resized_folder . DS . $id . DS . $suffix; if (!file_exists($abs_images_dir . DS . $resized_folder)) { mkdir($abs_images_dir . DS . $resized_folder, 0777); } if (!file_exists($abs_images_dir . DS . $resized_folder . DS . $id)) { mkdir($abs_images_dir . DS . $resized_folder . DS . $id, 0777); } if (!file_exists($abs_new_img_dir)) { mkdir($abs_new_img_dir, 0777); } $sizeOptions = array('width' => $width, 'height' => $height, 'method' => THUMBNAIL_METHOD_SCALE_MIN); $p = str_replace('.jpg', '-' . $suffix . '.jpg', $abs_new_img_dir . DS . basename($image)); if (!file_exists($p)) { $thumb = new Thumbnail(); $resizeImage = $thumb->render($image, $sizeOptions); @imageJpeg($resizeImage, $p, 90); @imagedestroy($resizeImage); } return $images_dir . DS . $resized_folder . DS . $id . DS . $suffix . DS . str_replace('.jpg', '-' . $suffix . '.jpg', basename($image)); }
/** * Вывод сгенерированного изображения * Данный метод вызывает метод render() и выводит сгенерированное изображение в браузер или файл * * @param mixed $input Имя файла, изображение-строка или GD-resource * @param mixed $output Имя файла-результата. Если null - будет выведено в браузер * @param array $options Массив настроек * <pre> * width int Ширина изображения-результата * height int Высота изображения-результата * percent number Размер изображения-результата в процентах от исходного * method int Метод ресайза * halign int Горизонтальное выравнивание * valign int Вертикальное выравнивание * check_size int Производить проверку размеров (в этом случае изображение не ресайзится, если необходимый размер больше исходного) * quality int Качество выдаваемого изображения. 0-100 * x int Растояние в пикселях от левого края, для обрезания * y int Растояние в пикселях от верхнего края, для обрезания * </pre> * * @return boolean TRUE on success or FALSE on failure. * @access public */ public static function output($input, $output = null, $options = array()) { // Load source file and render image $renderImage = Thumbnail::render($input, $options); if (!$renderImage) { throw new joosImageLibrariesException('Error rendering image'); } // Set output image type // By default PNG image $type = isset($options['type']) ? $options['type'] : IMAGETYPE_JPEG; $quality = isset($options['quality']) ? $options['quality'] : ($type == IMAGETYPE_PNG ? 8 : 80); $quality = $type == IMAGETYPE_PNG ? (int) $quality / 10 : $quality; // что бы не указывать в параметрах 0-100 для JPG и 0-9 для PNG - можно всегда 0-100, а тут подправим // Before output to browsers send appropriate headers if (empty($output)) { $content_type = image_type_to_mime_type($type); if (!headers_sent()) { joosRequest::send_headers('Content-Type: ' . $content_type); } else { throw new joosImageLibrariesException('Headers have already been sent. Could not display image.'); } } else { $content_type = 'ERROR'; } switch ($type) { case IMAGETYPE_GIF: $result = empty($output) ? imagegif($renderImage) : imagegif($renderImage, $output); break; case IMAGETYPE_PNG: $result = empty($output) ? imagepng($renderImage) : imagepng($renderImage, $output, $quality); break; case IMAGETYPE_JPEG: $result = empty($output) ? imagejpeg($renderImage) : imagejpeg($renderImage, $output, $quality); break; default: throw new joosImageLibrariesException('Image type ' . $content_type . ' not supported by PHP'); } if (!$result) { throw new joosImageLibrariesException('Error output image', E_USER_NOTICE); } // освобождаем память, выделенную для изображения imagedestroy($renderImage); return true; }