/** * Optimise a media image * * @param Media $media * * @return mixed */ public function optimise($media) { $info = getimagesize($media->getPath()); switch ($info['mime']) { case 'image/jpeg': exec("jpegoptim --strip-all \"{$media->getPath()}\""); break; case 'image/gif': $image = imagecreatefromgif($media->getPath()); imagepng($image, $media->getPath()); exec("optipng \"{$media->getPath()}\""); break; case 'image/png': exec("optipng \"{$media->getPath()}\""); break; default: return $media; break; } return $media; }
/** * Optimise a media image * * @param Media $media * * @return mixed */ public function optimise($media) { $info = getimagesize($media->getPath()); switch ($info['mime']) { case 'image/jpeg': $image = imagecreatefromjpeg($media->getPath()); break; case 'image/gif': $image = imagecreatefromgif($media->getPath()); break; case 'image/png': $image = imagecreatefrompng($media->getPath()); break; default: return $media; break; } //save file imagejpeg($image, $media->getPath(), $this->quality); //return destination file return $media; }
/** * Check if a media object fits the size profile * * @param Media $media * * @return bool */ public function fits(Media $media) { $fits = true; $img = @getimagesize($media->getPath()); if ($img === false) { return false; } if (!$this->isXInherited()) { if ($img[0] !== $this->x) { $fits = false; } } if (!$this->isYInherited()) { if ($img[1] !== $this->y) { $fits = false; } } return $fits; }
/** * @param \LineStorm\MediaBundle\Model\Media $media * @param array $api */ function __construct(\LineStorm\MediaBundle\Model\Media $media, array $api = array()) { $this->alt = $media->getAlt(); $this->credits = $media->getCredits(); $this->hash = $media->getHash(); $this->id = $media->getId(); $this->name = $media->getName(); $this->nameOriginal = $media->getNameOriginal(); $this->src = $media->getSrc(); $this->title = $media->getTitle(); $this->path = $media->getPath(); $this->category = $media->getCategory(); $this->_api = $api; }
/** * Resize a media object to a set size, returns the persisted media version object * * @param Media $media * @param MediaResizeProfile $resizeProfile * * @return MediaVersion */ protected function resizeImage(Media $media, MediaResizeProfile $resizeProfile) { $imagePath = pathinfo($media->getPath()); $imageSize = getimagesize($media->getPath()); // clear old versions $repo = $this->em->getRepository($this->getVersionEntityClass()); $query = $repo->createQueryBuilder('mv')->delete()->where('mv.media = :media')->andWhere('mv.resizeProfile = :profile')->setParameters(array('media' => $media, 'profile' => $resizeProfile))->getQuery(); $query->execute(); $image = new ImageResize($media->getPath()); $resizeMethod = $imageSize[0] >= $imageSize[1] ? ImageResize::RESIZE_MAX_WIDTH : ImageResize::RESIZE_MAX_HEIGHT; $image->resizeTo($resizeProfile->getWidth(), $resizeProfile->getHeight(), $resizeMethod); $newWidth = $image->getResizeWidth(); $newHeight = $image->getResizeHeight(); $filename = "{$imagePath['filename']}_{$newWidth}_x_{$newHeight}.{$imagePath['extension']}"; $resizedImagePath = "{$imagePath['dirname']}" . DIRECTORY_SEPARATOR . "{$filename}"; $image->saveImage($resizedImagePath); if (file_exists($resizedImagePath)) { $class = $this->getVersionEntityClass(); /** @var MediaVersion $mediaVersion */ $mediaVersion = new $class(); $mediaVersion->setMedia($media); $mediaVersion->setResizeProfile($resizeProfile); $mediaVersion->setPath($resizedImagePath); $mediaVersion->setSrc($this->storeDirectory . $filename); $this->optimiser->optimise($mediaVersion); $this->em->persist($mediaVersion); $this->em->flush($mediaVersion); return $mediaVersion; } }
/** * @param Media $media */ public function addMedia(Media $media) { $this->media[] = $media; $media->setCategory($this); }