/** * Main constructor * * @param Conf $conf Viewer configuration * @param array $ptypes Pyramidal types */ public function __construct($conf, $ptypes) { if (!extension_loaded($this->extensionName())) { Analog::error($this->missingLib()); throw new \RuntimeException($this->missingLib()); } $this->conf = $conf; $this->_pyramidal_types = $ptypes; }
/** * Resize the image if it exceed max allowed sizes * * @param string $source the source image * @param string $dest the destination image. * @param string $format the format to use * * @return void */ public function resize($source, $dest, $format) { $gdinfo = gd_info(); $fmts = $this->conf->getFormats(); $fmt = $fmts[$format]; $h = $fmt['height']; $w = $fmt['width']; switch ($this->img_type) { case IMAGETYPE_JPEG: if (!$gdinfo['JPEG Support']) { Analog::log('[' . $this->get_class($this) . '] GD has no JPEG Support - ' . 'pictures could not be resized!', Analog::ERROR); return false; } break; case IMAGETYPE_PNG: if (!$gdinfo['PNG Support']) { Analog::log('[' . $this->get_class($this) . '] GD has no PNG Support - ' . 'pictures could not be resized!', Analog::ERROR); return false; } break; case IMAGETYPE_GIF: if (!$gdinfo['GIF Create Support']) { Analog::log('[' . $this->get_class($this) . '] GD has no GIF Support - ' . 'pictures could not be resized!', Analog::ERROR); return false; } break; default: Analog::error('Current image type cannot be resized'); return false; } list($cur_width, $cur_height, $cur_type, $curattr) = getimagesize($source); $ratio = $cur_width / $cur_height; // calculate image size according to ratio if ($cur_width > $cur_height) { $h = $w / $ratio; } else { $w = $h * $ratio; } $thumb = imagecreatetruecolor($w, $h); $image = $this->_getImageAsResource($source); switch ($this->img_type) { case IMAGETYPE_JPEG: imagecopyresampled($thumb, $image, 0, 0, 0, 0, $w, $h, $cur_width, $cur_height); imagejpeg($thumb, $dest); break; case IMAGETYPE_PNG: // Turn off alpha blending and set alpha flag. That prevent alpha // transparency to be saved as an arbitrary color (black in my tests) imagealphablending($thumb, false); imagealphablending($image, false); imagesavealpha($thumb, true); imagesavealpha($image, true); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $w, $h, $cur_width, $cur_height); imagepng($thumb, $dest, 9); break; case IMAGETYPE_GIF: imagecopyresampled($thumb, $image, 0, 0, 0, 0, $w, $h, $cur_width, $cur_height); imagegif($thumb, $dest); break; case IMAGETYPE_TIFF_II: case IMAGETYPE_TIFF_MM: /** Gd cannot resize TIFF images. */ throw new \RuntimeException(_('TIFF images cannot be resized using Gd library!')); break; } }
if ($remote_infos !== false) { $v->setData('remote_method', $remote_infos['method']); $v->setData('remote_uri', $remote_infos['uri']); } }); //set default conditions Route::setDefaultConditions(array('image' => '.+\\.[a-zA-Z]{3,4}', 'series' => '.+', 'format' => 'full|' . implode('|', array_keys($conf->getFormats())))); //404 handler $app->notFound(function () use($app) { $app->render('404.html.twig'); }); //custom error handler $app->error(function (\Exception $e) use($app, $conf, $app_base_url) { $resuUri = $app->request()->getResourceUri(); $etype = get_class($e); Analog::error('exception \'' . $etype . '\' with message \'' . $e->getMessage() . '\' in ' . $e->getFile() . ':' . $e->getLine() . "\nStack trace:\n" . $e->getTraceAsString()); if ((substr($resuUri, 0, 10) === '/ajax/img/' || substr($resuUri, 0, 21) === '/ajax/representative/') && APP_DEBUG !== true) { $format = 'default'; preg_match('/.*\\/format\\/(.*)/', $resuUri, $matches); if (isset($matches[1])) { $format = $matches[1]; } $picture = new Picture($conf, DEFAULT_PICTURE, $app_base_url); $display = $picture->getDisplay($format); $response = $app->response(); foreach ($display['headers'] as $key => $header) { $response[$key] = $header; } $response->body($display['content']); } else { $app->render('50x.html.twig', array('exception' => $e));
/** * Get image informations for a specific format * * @param string $format Required format * * @return array */ private function _checkImageFormat($format) { $name_path = explode('/', $this->_name); $image_name = array_pop($name_path); $prepared_path = $this->_conf->getPreparedPath() . $format; $prepared_path .= '/' . $this->_getRelativePath($image_name); $image_path = $prepared_path . $image_name; $flagChangeImage = false; if (file_exists($image_path)) { $dateImage = new \DateTime(); $datePreparedImage = new \DateTime(); $dateImage->setTimestamp(filectime($this->_full_path)); $datePreparedImage->setTimestamp(filectime($image_path)); if ($dateImage > $datePreparedImage) { $flagChangeImage = true; } } if (!file_exists($image_path) || $flagChangeImage) { //prepared image does not exists yet if (file_exists($this->_conf->getPreparedPath()) && is_dir($this->_conf->getPreparedPath()) && is_writable($this->_conf->getPreparedPath())) { if (!file_exists($prepared_path)) { mkdir($prepared_path, 0755, true); } $this->_prepareImage($image_path, $format); } else { Analog::error(str_replace('%path', $this->_conf->getPreparedPath(), _('%path does not exists or is not writable!'))); //let's serve original image... return array($this->_full_path, filesize($this->_full_path)); } } return array($image_path, filesize($image_path)); }
/** * Default footer * * @return void */ public function Footer() { $image = $this->_conf->getPrintFooter($this->CurOrientation); if (file_exists($image)) { $this->SetY(280); $this->writeHTML('<img src="' . $image . '"/>'); $this->_footer_height = ceil($this->getY()); } else { Analog::error(str_replace('%file', $image, 'File %file does not exists!')); } }
/** * Set image from its position in series * * @param int $pos Image position * * @return boolean */ public function setNumberedImage($pos) { $pos = $pos - 1; if (isset($this->_content[$pos])) { return $this->setImage($this->_content[$pos]); } else { Analog::error(str_replace('%pos', $pos, _('No image at position %pos!'))); return false; } }