/** * Create a thumbnail for the given file with the given width and height. * * @param string $name name of the image to create the thumbnail of * @param string $fullPath absolute path to the image to create the thumbnail of * @param int $width width to resize to (Set to 0 to preserve aspect ratio) * @param int $height height to resize to (Set to 0 to preserve aspect ratio) * @param bool $exact This will preserve aspect ratio by using a crop after the resize * @return string path where the thumbnail was created * @throws phMagickException * @throws Zend_Exception */ public function createThumbnailFromPath($name, $fullPath, $width, $height, $exact = true) { /** @var SettingModel $settingModel */ $settingModel = MidasLoader::loadModel('Setting'); $provider = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_PROVIDER_KEY, $this->moduleName); $useThumbnailer = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_USE_THUMBNAILER_KEY, $this->moduleName); $preprocessedFormats = array('mha', 'nrrd'); $ext = strtolower(substr(strrchr($name, '.'), 1)); if ($useThumbnailer === 1 && $provider === 'phmagick' && in_array($ext, $preprocessedFormats)) { // pre-process the file to get a temporary JPEG file and then feed it to ImageMagick later. $preprocessedJpeg = $this->preprocessByThumbnailer($name, $fullPath); if (isset($preprocessedJpeg) && file_exists($preprocessedJpeg)) { $fullPath = $preprocessedJpeg; $ext = strtolower(substr(strrchr($preprocessedJpeg, '.'), 1)); } } // create destination $tmpPath = UtilityComponent::getTempDirectory('thumbnailcreator'); $format = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_FORMAT_KEY, $this->moduleName); if ($format === 'jpeg') { $format = MIDAS_THUMBNAILCREATOR_FORMAT_JPG; } /** @var RandomComponent $randomComponent */ $randomComponent = MidasLoader::loadComponent('Random'); $destination = $tmpPath . '/' . $randomComponent->generateInt() . '.' . $format; while (file_exists($destination)) { $destination = $tmpPath . '/' . $randomComponent->generateInt() . '.' . $format; } $pathThumbnail = $destination; if ($provider === 'phmagick') { $imageMagickPath = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_IMAGE_MAGICK_KEY, $this->moduleName); switch ($ext) { case 'pdf': case 'mpg': case 'mpeg': case 'mp4': case 'm4v': case 'avi': case 'mov': case 'flv': case 'rm': // If this is a video, we have to have the file extension, so symlink it if (function_exists('symlink') && symlink($fullPath, $fullPath . '.' . $ext)) { $p = new phmagick('', $pathThumbnail); $p->setImageMagickPath($imageMagickPath); $p->acquireFrame($fullPath . '.' . $ext, 0); if ($exact) { // preserve aspect ratio by performing a crop after the resize $p->resizeExactly($width, $height); } else { $p->resize($width, $height); } unlink($fullPath . '.' . $ext); } break; default: // Otherwise it is just a normal image $p = new phmagick($fullPath, $pathThumbnail); $p->setImageMagickPath($imageMagickPath); if ($exact) { // preserve aspect ratio by performing a crop after the resize $p->resizeExactly($width, $height); } else { $p->resize($width, $height); } break; } // delete temporary file generated in pre-process step if (isset($preprocessedJpeg) && file_exists($preprocessedJpeg)) { unlink($preprocessedJpeg); } } elseif ($provider === MIDAS_THUMBNAILCREATOR_PROVIDER_GD || $provider === MIDAS_THUMBNAILCREATOR_PROVIDER_IMAGICK) { try { $manager = new \Intervention\Image\ImageManager(array('driver' => $provider)); $image = $manager->make($fullPath); if ($height === 0) { $image->widen($width); } elseif ($width === 0) { $image->heighten($height); } else { $image->resize($width, $height); } $image->save($pathThumbnail); } catch (\RuntimeException $exception) { Zend_Registry::get('logger')->err($exception->getMessage()); throw new Zend_Exception('Thumbnail creation failed'); } } else { throw new Zend_Exception('No thumbnail provider has been selected'); } return $pathThumbnail; }
public function edit($id) { $ads = Model\Portal\Ads::where('code', $id)->first(); if ($ads) { $this->form_validation->set_rules('show', 'Tampil', 'required'); if ($this->form_validation->run() == FALSE) { $this->template->build('edit', compact('ads')); } else { if (isset($_FILES['image'])) { $manager = new Intervention\Image\ImageManager(); $image = $manager->make($_FILES['image']['tmp_name']); $image->resize($ads->width, $ads->height); $image->save(PATH_ADS . '/ads_' . $ads->code . '.jpg'); $ads->image = 'ads_' . $ads->code . '.jpg'; } $ads->show = $this->input->post('show'); $ads->link = $this->input->post('link'); $ads->save(); set_message_success('Iklan berhasil diperbarui.'); redirect('ads', 'refresh'); } } else { set_message_error('Iklan tidak ada'); redirect('ads', 'refresh'); } }
public function uploadFile($file, $type = null) { $w = null; $h = null; switch ($type) { case 'avatar': default: $h = 300; $prefix = "avatars"; break; } include_once '../vendor/intervention/image/src/Intervention/Image/ImageManager.php'; $image = new \Intervention\Image\ImageManager(); $img = $image->make($file->getRealPath()); $ext = $file->getClientOriginalExtension(); $img->resize($w, $h, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); $fname = $this->generateName(); $fname .= "." . $ext; switch (config('filesystems.default')) { case 'local': Storage::put($prefix . "/" . $fname, $img); break; case 's3': Storage::put($prefix . "/" . $fname, $img->stream()->__toString()); break; } // storage_path always shows local path, even when filesystem set to s3 // Storage facade does not return the path to remote file, hence for now - fixed one $path = "https://s3.eu-central-1.amazonaws.com/flocc"; return $path . "/" . $prefix . "/" . $fname; }
/** * Get image either from cache or directly processed * and save image in cache if it's not saved yet * * @param int $lifetime * @param bool $returnObj * @return mixed */ public function get($lifetime = null, $returnObj = false) { $lifetime = is_null($lifetime) ? $this->lifetime : intval($lifetime); $key = $this->checksum(); // try to get image from cache $cachedImageData = $this->cache->get($key); // if imagedata exists in cache if ($cachedImageData) { // transform into image-object if ($returnObj) { $image = $this->manager->make($cachedImageData); $cachedImage = new CachedImage(); return $cachedImage->setFromOriginal($image, $key); } // return raw data return $cachedImageData; } else { // process image data $image = $this->process(); // encode image data only if image is not encoded yet $image = $image->encoded ? $image->encoded : $image->encode(); // save to cache... $this->cache->put($key, (string) $image, $lifetime); // return processed image return $returnObj ? $image : (string) $image; } }
public function createThumb($fileKey) { list($width, $height) = getimagesize('C:\\xampp\\htdocs\\filehost\\web\\files/' . $fileKey); $imageManager = new \Intervention\Image\ImageManager(array('driver' => 'gd')); $image = $imageManager->make('C:\\xampp\\htdocs\\filehost\\web\\files\\' . $fileKey); if ($width >= $height) { $image->resize(250, null, function ($constraint) { $constraint->aspectRatio(); }); } else { $image->resize(null, 250, function ($constraint) { $constraint->aspectRatio(); }); } $image->save('C:\\xampp\\htdocs\\filehost\\web\\files\\thumb\\' . $fileKey); }
/** * Get image either from cache or directly processed * and save image in cache if it's not saved yet * * @param int $lifetime * @param bool $returnObj * @return mixed */ public function get($lifetime = null, $returnObj = false, $lastModified = null) { $lifetime = is_null($lifetime) ? $this->lifetime : intval($lifetime); $key = $this->checksum(); // try to get image from cache $cachedImageData = unserialize($this->cache->get($key)); // if imagedata exists in cache if ($cachedImageData) { if (!empty($lastModified) && $this->checkLastModified($lastModified, $cachedImageData['last_modified'])) { return new NotModified(); } // transform into image-object if ($returnObj) { $image = $this->manager->make($cachedImageData['image']); $cachedImage = new CachedImage(); return $cachedImage->setFromOriginal($image, $key); } // return raw data return $cachedImageData['image']; } else { // process image data $image = $this->process(); // encode image data only if image is not encoded yet $encoded = $image->encoded ? $image->encoded : (string) $image->encode(); $data = serialize(['last_modified' => gmdate("D, d M Y H:i:s \\G\\M\\T", time()), 'image' => $encoded]); // save to cache... $this->cache->put($key, $data, $lifetime); // return processed image return $returnObj ? $image : $encoded; } }
/** * 本地生成水印api * @param mixed $img_url 原图url * @param mixed $watermark_img_url 水印url * @param mixed $config 水印配置,默认为 array( * 'position' => 'center', // top-left, top, top-right, left, center, right, bottom-left, bottom, bottom-right * 'width_scale' => 0.5, // 水印占据原图宽度 * 'x' => 0, // X offset * 'y' => 0, // Y offset * ) * @access public * * @return void */ static function local_watermark_api($img_url, $watermark_img_url, $config = []) { $config = array_merge(['position' => 'center', 'width_scale' => 0.5, 'x' => 0, 'y' => 0], $config); import('Common.Util.ImageManager'); $image = new \Intervention\Image\ImageManager(['driver' => 'imagick']); $img = $image->make($img_url); $mime = $img->mime(); $watermark = $image->make($watermark_img_url); $watermark->widen(intval($img->width() * $config['width_scale'])); $img->insert($watermark, $config['position'], $config['x'], $config['y']); switch ($mime) { case 'image/png': $type = 'png'; break; case 'image/gif': $type = 'gif'; break; case 'image/jpeg': default: $type = 'jpeg'; break; } return $img->encode($type); }
public function uploadPhotoAlbum() { $files = $this->getFiles(); if (count($files) < 1) { return false; } $fileLink = $this->getDir() . '/temp/' . rand(0, 10000) . time() . md5($files[0]->getName()) . '.jpg'; $files[0]->moveTo($fileLink); /* Optimize */ $factory = new \ImageOptimizer\OptimizerFactory(); $optimizer = $factory->get('jpegoptim'); $filepath = $fileLink; $optimizer->optimize($filepath); /* End Optimize */ $manger = new \Intervention\Image\ImageManager(); $image = $manger->make($fileLink); //iphone and android if (($image->mime === 'image/jpg' || $image->mime === 'image/jpeg') && $image->exif('Orientation')) { $this->orientate($image, $image->exif('Orientation')); } $image = $this->convertToThumbnail($image); $image->save($fileLink, 80); $locationData = (new fileStorage())->upload($fileLink); return $locationData; }