public function __toString() { if ($this->adaptive) { $this->gd->adaptiveResize($this->width, $this->height); } else { $this->gd->resize($this->width, $this->height); } return sprintf('data:%s;base64,%s', $this->gd->getFormat(), base64_encode($this->gd->getImageAsString())); }
/** * Get resized image * @param ImageParams $params * @return MongoGridFSFile */ public function get(ImageParams $params = null) { // Get original image or file if it is not image if (!$params || !$this->isImage($this->filename)) { return $this->_get(); } // Get resized image $params->isTemp = true; $result = $this->_get($params->toArray()); // Resize and store if not found if (!$result) { $result = $this->_get(); if (!$result) { throw new Exception('File not found'); } $originalFilename = $result->file['filename']; $fileName = tempnam('/tmp/', __CLASS__); $result->write($fileName); $image = new GD($fileName); if ($params->adaptive) { $image->adaptiveResize($params->width, $params->height)->save($fileName); } else { $image->resize($params->width, $params->height)->save($fileName); } $this->_set($fileName, $originalFilename, $params->toArray()); unlink($fileName); return $this->_get($params->toArray()); } return $result; }
public function createThumbs() { $path = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolvePath(); foreach ($this->thumbs as $profile => $config) { $thumbPath = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolveThumbPath($profile); if (!is_file($thumbPath)) { /** @var GD $thumb */ $thumb = new GD($path); if (isset($config['resizeUp'])) { $thumb->setOptions(array('resizeUp' => $config['resizeUp'])); } if (isset($config['adaptive']) && $config['adaptive'] === false) { $thumb->resize($config['width'], $config['height']); } else { //By default we do adaptiveResize $thumb->adaptiveResize($config['width'], $config['height']); } @mkdir(pathinfo($thumbPath, PATHINFO_DIRNAME), 777, true); $thumb->save($thumbPath); } } }
public function createThumbs() { $path = $this->getUploadedFilePath($this->attribute); if (file_exists($path)) { foreach ($this->thumbs as $profile => $config) { $thumbPath = static::getThumbFilePath($this->attribute, $profile); if (!is_file($thumbPath)) { /** @var GD $thumb */ $thumb = new GD($path); $dimensions = $thumb->getCurrentDimensions(); if ($dimensions['width'] > $dimensions['height']) { $thumb->resize($config['width'], $config['height']); } else { $thumb->resize($config['height'], $config['width']); } FileHelper::createDirectory(pathinfo($thumbPath, PATHINFO_DIRNAME), 0775, true); $thumb->save($thumbPath); if (isset($config['watermark'])) { $watermarkedImage = Image::watermark($thumbPath, $config['watermark']); $watermarkedImage->save($thumbPath); } } } } }
public function render($view = 'index', $data = array()) { $view = urldecode($view); $contentPath = $this->getOwner()->getContentPath(); $rootPath = $this->getOwner()->getRootPath(); $matches = []; if (preg_match('~@\\((\\d+)x(\\d+)\\)([a-z]{1})?$~', $view, $matches)) { $this->width = $matches[1]; $this->height = $matches[2]; if (isset($matches[3])) { $this->options = preg_split('~~', $matches[3], PREG_SPLIT_NO_EMPTY); } $pattern = preg_quote($matches[0]); $baseView = preg_replace("~{$pattern}\$~", '', $view); } $thumbName = sprintf('%s/%s.%s', $rootPath, $view, $this->extension); // Get thumbnail dir for later use $thumbDir = dirname($thumbName); // Try to make a thumbnail dir if (!file_exists($thumbDir)) { @mkdir($thumbDir, 0777, true); } $baseExt = str_replace('thumb.', '', $this->extension); $fileName = sprintf('%s/%s.%s', $contentPath, $baseView, $baseExt); if (!file_exists($fileName)) { throw new NotFoundException(sprintf('File not found: `%s`', $fileName)); } if (!file_exists($thumbName)) { $image = new GD($fileName); if (in_array(self::OptionCrop, $this->options)) { $image->adaptiveResize($this->width, $this->height); } else { $image->resize($this->width, $this->height); } // ensure we can write into dir or overwrite a file if (is_writeable($thumbDir) || is_writeable($thumbName)) { $image->save($thumbName); } } $info = new SplFileInfo($thumbName); $size = $info->getSize(); if ($size > 0) { header(sprintf('Content-Length: %d', $size)); } switch (strtolower($info->getExtension())) { case 'gif': header('Content-type: image/gif'); break; case 'jpg': header('Content-type: image/jpeg'); break; case 'png': case 'string': header('Content-type: image/png'); break; } header(sprintf('ETag: %s', md5($thumbName))); header(sprintf('Last-Modified: %s', gmdate('D, d M Y H:i:s \\G\\M\\T', $info->getMTime()))); header(sprintf('Content-Disposition: filename="%s"', basename($fileName))); // Cache it header('Pragma: public'); header('Cache-Control: max-age=86400'); header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 86400)); echo file_get_contents($thumbName); exit; }
/** * @param Idea $idea * @param $fileData * @param null $alternativeName */ public function addFileToIdea(Idea $idea, $fileData, $alternativeName = null) { //Parse first the type of file to see in which table it should be stored $mimeType = new MimeType(); $mimeType->isValid($fileData); /* * Use the fileSize validator to validate the size */ $fileSize = new FilesSize(PHP_INT_MAX); $fileSize->isValid($fileData); $contentType = $this->getGeneralService()->findContentTypeByContentTypeName($fileData['type']); switch ($mimeType->type) { case 'application/pdf': //All treated as document //All treated as document case 'application/msword': //All treated as document //All treated as document case 'application/vnd.ms-excel': //All treated as document //All treated as document case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': //All treated as document //All treated as document case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': //All treated as document //All treated as document case 'application/vnd.openxmlformats-officedocument.presentationml.presentation': $ideaDocument = new Document(); $ideaDocument->setIdea($idea); $ideaDocument->setFilename($fileData['name']); $ideaDocument->setSize($fileSize->size); if (is_null($alternativeName)) { $ideaDocument->setDocument($fileData['name']); } else { $ideaDocument->setDocument($alternativeName); } $ideaDocumentObject = new DocumentObject(); $ideaDocumentObject->setObject(file_get_contents($fileData['tmp_name'])); $ideaDocument->setContentType($contentType); $ideaDocumentObject->setDocument($ideaDocument); $this->newEntity($ideaDocumentObject); break; case 'image/jpeg': case 'image/png': $ideaImage = new Image(); $ideaImage->setIdea($idea); if (is_null($alternativeName)) { $ideaImage->setImage($fileData['name']); } else { $ideaImage->setImage($alternativeName); } $ideaImage->setSize($fileSize->size); $ideaImage->setContentType($contentType); $ideaImageObject = new ImageObject(); $ideaImageObject->setObject(file_get_contents($fileData['tmp_name'])); $thumb = new GD($fileData['tmp_name']); $thumb->resize(200); $ideaImageObject->setThumb($thumb->getImageAsString()); $ideaImageObject->setImage($ideaImage); $this->newEntity($ideaImageObject); break; } }