/** * @param $width * @param $path * @param Media $media * @return mixed */ public function getMin($width, $path, Media $media) { $this->tmp = Storage::disk('local'); $img = Cache::remember('img-' . $width . '-' . $path, 60, function () use($width, $path, $media) { $img = Image::make($media->getImage($path)); $img->resize($width, null, function (Constraint $constraint) { $constraint->aspectRatio(); }); $name = str_random(40) . '.jpg'; $data = $img->encode('jpg', 95); $this->tmp->put('tmpimage/' . $name, $data); $factory = new \ImageOptimizer\OptimizerFactory(); $optimizer = $factory->get('jpegoptim'); $filepath = storage_path() . '/app/tmpimage/' . $name; $optimizer->optimize($filepath); $img = Image::make($this->tmp->get('tmpimage/' . $name)); $this->tmp->delete('tmpimage/' . $name); return $img->response(); }); return $img; }
/** * _optimizeImage method * * Cache\[optimize] images in cache folder * * @param string $imgPath path to source image that will be cached\[optimized] * @return void */ protected function _optimizeImage($imgPath = null) { $factory = new \ImageOptimizer\OptimizerFactory(); $optimizer = $factory->get(); $optimizer->optimize($imgPath); }
public function __construct() { $factory = new \ImageOptimizer\OptimizerFactory(array('optipng_bin' => config('imageoptimizer.optipng_path'), 'jpegoptim_bin' => config('imageoptimizer.jpegoptim_path'), 'ignore_errors' => config('imageoptimizer.ignore_errors'))); $this->pngOptimzer = $factory->get('optipng'); $this->jpgOptimzer = $factory->get('jpegoptim'); }
function preview($adress, $param1 = false, $param2 = false) { $orig_params = $adress; if (is_array($adress)) { if (isset($adress['height']) || isset($adress[2])) { if (isset($adress['width'])) { $width = $adress['width']; } else { $width = $adress[1]; } if (isset($adress['height'])) { $height = $adress['height']; } else { $height = $adress[2]; } $adress = $adress[0]; } else { $num = 1; if (isset($adress[1])) { $num = $adress[1]; } $adress = $adress[0]; if ($adress == '') { return ''; } $ext = strtolower(strrchr($adress, '.')); if (!in_array($ext, array('.gif', '.jpg', '.png', '.jpeg'))) { return ''; } return substr($adress, 0, strrpos($adress, "/") + 1) . ".thumbs/preview" . $num . "_" . substr($adress, strrpos($adress, "/") + 1); } //Массив значений } else { if ($param2 === false) { //обычная превью $num = 1; if ($param1 !== false) { $num = $param1; } if ($adress == '') { return ''; } $ext = strtolower(strrchr($adress, '.')); if (!in_array($ext, array('.gif', '.jpg', '.png', '.jpeg'))) { return ''; } return substr($adress, 0, strrpos($adress, "/") + 1) . ".thumbs/preview" . $num . "_" . substr($adress, strrpos($adress, "/") + 1); } else { //необычная превью без массива значений $width = $param1; $height = $param2; } } if ($adress == '') { return ''; } $ext = strtolower(strrchr($adress, '.')); if (!in_array($ext, array('.gif', '.jpg', '.png', '.jpeg'))) { return ''; } $not_resize_hash = ''; if ($orig_params['not_resize']) { $not_resize_hash = '_nrs_'; } if (is_array($orig_params) && isset($orig_params['watermark'])) { $is_watermark = true; $preview_adress = substr($adress, 0, strrpos($adress, "/") + 1) . ".thumbs/preview_watermark" . $not_resize_hash . md5($orig_params['watermark'] . $width . 'x' . $height . "_" . substr($adress, strrpos($adress, "/") + 1)) . $ext; } else { $is_watermark = false; $preview_adress = substr($adress, 0, strrpos($adress, "/") + 1) . ".thumbs/preview" . $not_resize_hash . $watermark_suffix . $width . 'x' . $height . "_" . substr($adress, strrpos($adress, "/") + 1); } //генерирование изображения при его отсуствии if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $preview_adress)) { //Создание превью $filename = $_SERVER['DOCUMENT_ROOT'] . $adress; if (!is_file($filename)) { return ''; } $dest = $_SERVER['DOCUMENT_ROOT'] . $preview_adress; $dest_folder = $_SERVER['DOCUMENT_ROOT'] . substr($adress, 0, strrpos($adress, "/") + 1) . ".thumbs"; if (!file_exists($dest_folder)) { mkdir($dest_folder); chmod($dest_folder, 0777); } $format = strtolower(substr(strrchr($filename, "."), 1)); switch ($format) { case 'gif': $type = "gif"; break; case 'png': $type = "png"; break; case 'jpg': $type = "jpg"; break; case 'jpeg': $type = "jpg"; break; default: return false; break; } if ($file_dimensions = getimagesize($filename)) { $file_type = strtolower($file_dimensions['mime']); switch ($file_type) { case 'image/gif': $img = ImageCreateFromGif($filename); break; case 'image/png': $img = ImageCreateFromPng($filename); imageSaveAlpha($img, true); break; case 'image/jpeg': case 'image/pjpeg': $img = ImageCreateFromJpeg($filename); break; } } if (empty($img)) { return false; } list($org_width, $org_height) = getimagesize($filename); $nch_org_width = $org_width; $nch_org_height = $org_height; $xoffset = 0; $yoffset = 0; if (strpos($height, 'in') !== false and strpos($width, 'in') !== false) { $height_temp = substr($height, 2); $width_temp = substr($width, 2); if ($org_width <= $width_temp && $org_height <= $height_temp && $orig_params['not_resize']) { $height = $org_height; $width = $org_width; } else { $h_index = $org_height / $height_temp; $w_index = $org_width / $width_temp; $index = $h_index; if ($h_index < $w_index) { $index = $w_index; } $width = round($org_width / $index); $height = round($org_height / $index); } } else { if (strpos($height, 'in' === 0)) { $height = substr($height, 2); } elseif (strpos($width, 'in' === 0)) { $width = substr($width, 2); } } if ($height == 'auto' && $width == 'auto') { $height = $org_height; $width = $org_width; } else { if ($height == 'auto') { $height = round($width * ($org_height / $org_width)); } else { if ($width == 'auto') { $width = round($height * ($org_width / $org_height)); } } } if ($width / $height < $org_width / $org_height) { $dy = 0; $xtmp = $org_width; $org_width = $width * $org_height / $height; $dx = 0.5 * ($xtmp - $org_width); $xoffset = $dx; $yoffset = $dy; } else { $dx = 0; $ytmp = $org_height; $org_height = $height * $org_width / $width; $dy = 0.5 * ($ytmp - $org_height); $xoffset = $dx; $yoffset = $dy; } if ($org_height <= $height && $org_width <= $width && $orig_params['not_resize']) { $height = $nch_org_height; $width = $nch_org_width; $xoffset = 0; $yoffset = 0; $org_height = $nch_org_height; $org_width = $nch_org_width; } $img_n = imagecreatetruecolor($width, $height); imagealphablending($img_n, false); imagesavealpha($img_n, true); $black = imagecolorallocate($img_n, 0, 0, 0); $black2 = imagecolorallocate($img, 0, 0, 0); imageSaveAlpha($img, true); imagecopyresampled($img_n, $img, 0, 0, $xoffset, $yoffset, $width, $height, $org_width, $org_height); /* watermark*/ if ($is_watermark) { $imgwater = ImageCreateFromPng($_SERVER['DOCUMENT_ROOT'] . $orig_params['watermark']); imagealphablending($imgwater, false); imageSaveAlpha($imgwater, true); imagealphablending($img_n, false); imageSaveAlpha($img_n, true); $watermark = new Watermark_creator(); $img_n = $watermark->create($img_n, $imgwater, 100); } if ($type == "gif") { imagegif($img_n, $dest); } elseif ($type == "jpg") { imagejpeg($img_n, $dest, 100); } elseif ($type == "png") { imagepng($img_n, $dest); } elseif ($type == "bmp") { imagewbmp($img_n, $dest); } if (isset($_ENV["DOIT_OPTIMIZE_IMAGES"]) && $_ENV["DOIT_OPTIMIZE_IMAGES"] == true) { if (isset($_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"])) { if ($type == "jpg" && isset($_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"]) && isset($_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"]['JPG'])) { $path = $_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"]['JPG']; $path = str_replace("#SOURCE#", escapeshellarg($dest), $path); $path = str_replace("#DEST#", escapeshellarg($dest . '.tmp'), $path); exec($path); if (is_file($dest . '.tmp') && filesize($dest . '.tmp') != 0) { unlink($dest); rename($dest . '.tmp', $dest); } } elseif ($type == "png" && isset($_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"]) && isset($_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"]['PNG'])) { $path = $_ENV["DOIT_OPTIMIZE_IMAGES_EXTEND"]['PNG']; $path = str_replace("#SOURCE#", escapeshellarg($dest), $path); $path = str_replace("#DEST#", escapeshellarg($dest . '.tmp'), $path); exec($path); if (is_file($dest . '.tmp') && filesize($dest . '.tmp') != 0) { unlink($dest); rename($dest . '.tmp', $dest); } } } else { $factory = new \ImageOptimizer\OptimizerFactory(); $optimizer = $factory->get(); $res = $optimizer->optimize($dest); } } chmod($dest, 0777); } return $preview_adress; }
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; }
function save($temp_name, $field_name = false, $table = false, $id = false, $dir = false, $img_sizes = false, $field_name_n = false) { global $CFG; if ($temp_name) { $temp_name1 = $temp_name; $temp_name = $CFG->dirroot . $CFG->temp_file_location . $temp_name; $dir1 = $dir; $dir = $dir ? $CFG->dirroot . $dir : $CFG->dirroot . $CFG->default_upload_location; $dir = !stristr($dir, '/') ? $dir . '/' : $dir; $file_parts = explode('.', $temp_name); $ext = strtolower(end($file_parts)); if (!empty($_REQUEST['tokenizers'])) { unset($_REQUEST['tokenizers']); } DB::saveImageSizes($field_name, $_REQUEST['image_sizes'][$field_name]); $image_sizes = DB::getImageSizes($field_name); if ($id) { $ext1 = $ext; $i = DB::insert($table . '_files', array('f_id' => $id, 'ext' => $ext1, 'dir' => $dir1, 'old_name' => $temp_name1, 'field_name' => $field_name)); self::saveDescriptions($table, $field_name_n, $i); if (in_array($ext, $CFG->accepted_image_formats)) { $factory = new \ImageOptimizer\OptimizerFactory(array('ignore_errors' => false, 'jpegoptim_options' => array('--strip-all', '--all-progressive', '--max=75')), $logger); $optimizer = $factory->get('jpegoptim'); $ir = new ImageResize($temp_name, false, false, false); $ir->setHighQuality(); if ($_REQUEST['crop_images'][$field_name]) { $ir->setAutoCrop(true); } if (!is_array($img_sizes)) { $ir->save($dir . $table . '_' . $id . '_' . $i . '.' . $ext1, $ext1, 100); $optimizer->optimize($dir . $table . '_' . $id . '_' . $i . '.' . $ext1); if ($_REQUEST['encrypt_files'][$field_name]) { File::encrypt($dir . $table . '_' . $id . '_' . $i . '.' . $ext1); } } else { foreach ($image_sizes as $key => $size) { $ir->setSize($size['width'], $size['height']); $ir->save($dir . $table . '_' . $id . '_' . $i . '_' . $key . '.' . $ext1, 'jpeg', 100); $optimizer->optimize($dir . $table . '_' . $id . '_' . $i . '_' . $key . '.' . $ext1); if ($_REQUEST['encrypt_files'][$field_name]) { File::encrypt($dir . $table . '_' . $id . '_' . $i . '_' . $key . '.' . $ext1); } } } unlink($temp_name); return true; } else { if (rename($temp_name, $dir . $table . '_' . $id . '_' . $i . '.' . $ext)) { if ($_REQUEST['encrypt_files'][$field_name]) { File::encrypt($dir . $table . '_' . $id . '_' . $i . '.' . $ext); } return true; } } } else { if (rename($temp_name, $dir . $temp_name)) { if ($_REQUEST['encrypt_files'][$field_name]) { File::encrypt($dir . $dir . $temp_name); } return $dir . $temp_name; } } } else { return false; } }