Ejemplo n.º 1
0
 function process()
 {
     $param = $this->param;
     $image_path = $original_file = $this->image_path;
     $settings = $this->settings;
     $image_quality = intval($settings['image']['quality']);
     Utils::log($image_path, "Reading image");
     $img = Image::make($image_path);
     $mime = $img->mime();
     Utils::log($img->getWidth() . " X " . $img->getHeight(), "Dimensions");
     Utils::log($img->mime(), "Mime");
     Utils::log($image_quality, "Quality");
     // Calculate the correct dimensions. If necessary, avoid upscaling the image.
     $src_w = $img->getWidth();
     $src_h = $img->getHeight();
     if ($settings['image']['disable_upscaling'] == 'yes') {
         $dst_w = min($param->width, $src_w);
         $dst_h = min($param->height, $src_h);
     } else {
         $dst_w = $param->width;
         $dst_h = $param->height;
     }
     //$this->output_format = FORMAT_GIF;
     if ($this->output_format != 'default') {
         $img = $img->encode($this->output_format, $image_quality);
     }
     Utils::log($img->mime(), "Mime");
     // If there is no mode for the requested image, just read the image
     // from it's location (which may be external)
     // http://yajit.test/i/0/public/foo.jpg
     if ($param->mode == MODE_NONE) {
         if ($param->external && Image::getHttpResponseCode($original_file) != 200 || $param->external === FALSE && (!file_exists($original_file) || !is_readable($original_file))) {
             // Guess not, return 404.
             Page::renderStatusCode(Page::HTTP_STATUS_NOT_FOUND);
             trigger_error(sprintf('Image <code>%s</code> could not be found.', str_replace(DOCROOT, '', $original_file)), E_USER_ERROR);
             echo sprintf('Image <code>%s</code> could not be found.', str_replace(DOCROOT, '', $original_file));
             exit;
         }
     }
     switch ($param->mode) {
         // http://yajit.test/i/1/80/80/public/foo.jpg
         case MODE_RESIZE:
             // resize image to fixed size
             $img->resize($dst_w, $dst_h);
             break;
             // http://yajit.test/i/4/300/200/public/foo.jpg
         // http://yajit.test/i/4/300/200/public/foo.jpg
         case MODE_FIT:
             // resize image to fit size
             if ($param->height == 0) {
                 $ratio = $src_h / $src_w;
                 $dst_h = round($dst_w * $ratio);
             } else {
                 if ($param->width == 0) {
                     $ratio = $src_w / $src_h;
                     $dst_w = round($dst_h * $ratio);
                 }
             }
             $src_r = $src_w / $src_h;
             $dst_r = $dst_w / $dst_h;
             Utils::log("src_w: {$src_w} src_h: {$src_h} dst_w:{$dst_w} dst_h:{$dst_h} src_r:{$src_r} dst_r:{$dst_r}");
             if ($src_h <= $dst_h && $src_w <= $dst_w) {
                 //$image->applyFilter('resize', array($src_w, $src_h));
                 Utils::log("img->resize({$dst_w}, {$dst_h})");
                 $img->resize($dst_w, $dst_h);
                 break;
             }
             if ($src_h >= $dst_h && $src_r <= $dst_r) {
                 //$image->applyFilter('resize', array(NULL, $dst_h));
                 Utils::log("img->resize(null, {$dst_h})");
                 $img->resize(NULL, $dst_h, function ($constraint) {
                     $constraint->aspectRatio();
                 });
             }
             if ($src_w >= $dst_w && $src_r >= $dst_r) {
                 //$image->applyFilter('resize', array($dst_w, NULL));
                 Utils::log("img->resize({$dst_w}, null)");
                 $img->resize($dst_w, NULL, function ($constraint) {
                     $constraint->aspectRatio();
                 });
             }
             break;
             // http://yajit.test/i/2/500/500/2/public/foo.jpg
         // http://yajit.test/i/2/500/500/2/public/foo.jpg
         case MODE_RESIZE_CROP:
             if ($param->height == 0) {
                 $ratio = $src_h / $src_w;
                 $dst_h = round($dst_w * $ratio);
             } else {
                 if ($param->width == 0) {
                     $ratio = $src_w / $src_h;
                     $dst_w = round($dst_h * $ratio);
                 }
             }
             $src_r = $src_w / $src_h;
             $dst_r = $dst_w / $dst_h;
             if ($src_r < $dst_r) {
                 $img->resize($dst_w, NULL, function ($constraint) {
                     $constraint->aspectRatio();
                 });
             } else {
                 $img->resize(NULL, $dst_h, function ($constraint) {
                     $constraint->aspectRatio();
                 });
             }
             // http://yajit.test/i/3/500/500/2/public/foo.jpg
         // http://yajit.test/i/3/500/500/2/public/foo.jpg
         case MODE_CROP:
             //$image->applyFilter('crop', array($dst_w, $dst_h, $param->position, $param->background));
             $dst_w = $img->getWidth();
             $dst_h = $img->getHeight();
             $src_w = $img->getWidth();
             $src_h = $img->getHeight();
             $width = $param->width;
             $height = $param->height;
             Utils::log("{$dst_w}, {$dst_h}, {$width}, {$height}");
             if (!empty($width) && !empty($height)) {
                 $dst_w = $width;
                 $dst_h = $height;
             } else {
                 if (empty($height)) {
                     $ratio = $dst_h / $dst_w;
                     $dst_w = $width;
                     $dst_h = round($dst_w * $ratio);
                 } else {
                     if (empty($width)) {
                         $ratio = $dst_w / $dst_h;
                         $dst_h = $height;
                         $dst_w = round($dst_h * $ratio);
                     }
                 }
             }
             Utils::log("{$dst_w}, {$dst_h}, {$width}, {$height}");
             list($src_x, $src_y, $dst_x, $dst_y) = $this->__calculateDestSrcXY($dst_w, $dst_h, $src_w, $src_h, $src_w, $src_h, $param->position);
             Utils::log("{$src_x}, {$src_y}, {$dst_x}, {$dst_y}");
             $img->crop($dst_w, $dst_h, $dst_x, $dst_y);
             break;
     }
     if ($param->mode != MODE_NONE and $img->mime() == "image/jpeg") {
         $img->interlace();
     }
     $output_format = $this->output_format == "default" ? $img->mime() : $this->output_format;
     switch ($output_format) {
         case "image/jpeg":
             $format = FORMAT_JPG;
             break;
         case "image/png":
             $format = FORMAT_PNG;
             break;
         case "image/gif":
             $format = FORMAT_GIF;
             break;
         default:
             break;
     }
     if (CACHING and !file_exists($this->cache_file)) {
         Utils::log($this->cache_file, "SAVING CACHE FILE");
         $img->encode($format, $image_quality);
         $img->save($this->cache_file);
     }
     echo $img->response($format, $image_quality);
     exit;
 }