crop() public method

Crops image according to the given width, height and crop position
public crop ( integer $width, integer $height, boolean $allow_enlarge = false, integer $position = self::CROPCENTER ) : static
$width integer
$height integer
$allow_enlarge boolean
$position integer
return static
Ejemplo n.º 1
0
 /**
  * 修剪图片的大小
  * @param $originName
  * @param $targetName
  * @param $width
  * @param $height
  * @param bool $allow_enlarge 如果是true则可以将小于原始大小的图片放大
  */
 public static function resizeImage($originName, $targetName, $width, $height, $allow_enlarge = false, $quality = 100)
 {
     if (file_exists($originName)) {
         $image = new ImageResize($originName);
         $image->crop($width, $height, $allow_enlarge);
         $image->save($targetName, null, $quality);
     }
 }
Ejemplo n.º 2
0
 public function crop($images)
 {
     $this->makeRootDirectory();
     foreach ($images as $image) {
         foreach ($this->devices as $key => $value) {
             $img = new ImageResize($image);
             $img->crop($value[0], $value[1], false);
             $this->saveImage($img, $key, $image, $value[2]);
             unset($img);
         }
     }
     $directoryFullPath = "downloads/{$this->downloadDir}";
     $this->zipDirectory($directoryFullPath);
     $this->cleanUp($directoryFullPath);
     return $this->downloadDir;
 }
 /**
  * Returns a thumb url based on the url an size provided.
  *
  * @param string $url    Base url.
  * @param int    $width  Returned image width.
  * @param int    $height Returned image height.
  *
  * @return string
  */
 public static function thumb($url, $width = 0, $height = 0)
 {
     if (empty($width) || !is_numeric($width)) {
         $width = get_option('thumbnail_size_w');
     }
     if (empty($height) || !is_numeric($height)) {
         $height = get_option('thumbnail_size_h');
     }
     $info = pathinfo($url);
     if (!isset($info['extension'])) {
         $uniqid = explode('&', $info['filename']);
         $info['filename'] = $uniqid[count($uniqid) - 1];
         $info['extension'] = 'jpg';
     }
     $info['extension'] = explode('?', $info['extension'])[0];
     $cacheKey = preg_replace(['/:filename/', '/:width/', '/:height/'], [$info['filename'], $width, $height], get_option('thumbnail_cache_format', ':filename_:widthx:height'));
     return Cache::remember($cacheKey, get_option('thumbnail_cache_minutes', 43200), function () use($url, $width, $height, $info) {
         $upload_dir = wp_upload_dir();
         $assetPath = sprintf('/%s_%sx%s.%s', $info['filename'], $width, $height, $info['extension']);
         if (!file_exists($upload_dir['path'] . $assetPath)) {
             $image = new ImageResize($url);
             /// Process image
             $size = getimagesize($url);
             // Resize to fit wanted width is too small
             if ($size[0] < $width) {
                 $scaledPath = sprintf('/%s_%sx.%s', $info['filename'], $width, $info['extension']);
                 $image->interlace = 1;
                 $image->scale(ceil(100 + ($width - $size[0]) / $size[0] * 100));
                 $image->save($upload_dir['path'] . $scaledPath);
                 $image = new ImageResize($upload_dir['url'] . $scaledPath);
                 $size = getimagesize($upload_dir['url'] . $scaledPath);
             }
             // Resize to fit wanted height is too small
             if ($size[1] < $height) {
                 $scaledPath = sprintf('/%s_x%s.%s', $info['filename'], $height, $info['extension']);
                 $image->interlace = 1;
                 $image->scale(ceil(100 + ($height - $size[1]) / $size[1] * 100));
                 $image->save($upload_dir['path'] . $scaledPath);
                 $image = new ImageResize($upload_dir['url'] . $scaledPath);
             }
             // Final crop
             $image->crop($width, $height);
             $image->save($upload_dir['path'] . $assetPath);
         }
         return $upload_dir['url'] . $assetPath;
     });
 }
Ejemplo n.º 4
0
 public function testCropLargerNotAllowed()
 {
     $image = $this->createImage(200, 100, 'png');
     $resize = new ImageResize($image);
     $resize->crop(500, 500);
     $this->assertEquals(200, $resize->getDestWidth());
     $this->assertEquals(100, $resize->getDestHeight());
 }
Ejemplo n.º 5
0
 public function editarImagem($id = FALSE, $docente = FALSE)
 {
     if ($this->filtraInt($id)) {
         $diretorio = "upload/";
         move_uploaded_file($_FILES['imagem']["tmp_name"], $diretorio . $_FILES['imagem']["name"]);
         $resize = new ImageResize($diretorio . $_FILES['imagem']["name"]);
         $resize->crop(240, 320);
         unlink($diretorio . $_FILES['imagem']["name"]);
         $resize->save($diretorio . $_FILES['imagem']["name"]);
         $this->pessoa->setImagem($diretorio . $_FILES['imagem']["name"]);
         $this->pessoa->setId($id);
         $p = $this->pessoa->editar($this->pessoa);
         if ($p) {
             $this->redirecionar("docente/informacao/" . $docente);
         }
     }
     $this->redirecionar("docente");
 }
Ejemplo n.º 6
0
function imageProcessingEngine($a_width, $a_height, $a_count, $a_files, $a_name, $a_crop_pos)
{
    //Crop Images
    for ($i = 0; $i < $a_count; $i++) {
        echo "<hr>";
        echo "<b>" . ($i + 1) . "." . "Image Taken for Processing...</b>";
        echo "<br>" . $a_files[$i];
        echo "<br>Preparing images for resizing...";
        echo "<br>Under Processing...";
        $image = new ImageResize($a_files[$i]);
        $image->crop($a_width, $a_height, false, $a_crop_pos);
        $image->save($a_files[$i]);
        echo "<br>" . $a_files[$i];
        echo "<br>Image resized Successfully...";
    }
    echo "<hr><b>All image files are resized successfully</b><br>";
}
Ejemplo n.º 7
0
 /**
  * Returns a thumb url based on the url an size provided.
  *
  * @param string $url 	 Base url.
  * @param int    $width  Returned image width.
  * @param int    $height Returned image height.
  *
  * @return string
  */
 public static function thumb($url, $width = 0, $height = 0)
 {
     if (empty($width) || !is_numeric($width)) {
         $width = config('image.thumbs_width');
     }
     if (empty($height) || !is_numeric($height)) {
         $height = config('image.thumbs_height');
     }
     $info = pathinfo($url);
     if (!isset($info['extension'])) {
         $uniqid = explode('&', $info['filename']);
         $info['filename'] = $uniqid[count($uniqid) - 1];
         $info['extension'] = 'jpg';
     }
     $info['extension'] = explode('?', $info['extension'])[0];
     $cacheKey = preg_replace(['/:filename/', '/:width/', '/:height/'], [$info['filename'], $width, $height], config('image.cache_key_format'));
     return Cache::remember($cacheKey, config('image.cache_minutes'), function () use($url, $width, $height, $info) {
         $assetPath = sprintf('%s%s_%sx%s.%s', Config::get('image.thumbs_folder'), $info['filename'], $width, $height, $info['extension']);
         if (!file_exists(public_path() . $assetPath)) {
             $image = new ImageResize($url);
             /// Process image
             $size = getimagesize($url);
             // Resize to fit wanted width is too small
             if ($size[0] < $width) {
                 $scaledPath = sprintf('%s%s_%sx.%s', Config::get('image.thumbs_folder'), $info['filename'], $width, $info['extension']);
                 $image->interlace = 1;
                 $image->scale(ceil(100 + ($width - $size[0]) / $size[0] * 100));
                 $image->save(public_path() . $scaledPath);
                 $image = new ImageResize(URL::asset($scaledPath));
                 $size = getimagesize(URL::asset($scaledPath));
             }
             // Resize to fit wanted height is too small
             if ($size[1] < $height) {
                 $scaledPath = sprintf('%s%s_x%s.%s', Config::get('image.thumbs_folder'), $info['filename'], $height, $info['extension']);
                 $image->interlace = 1;
                 $image->scale(ceil(100 + ($height - $size[1]) / $size[1] * 100));
                 $image->save(public_path() . $scaledPath);
                 $image = new ImageResize(URL::asset($scaledPath));
             }
             // Final crop
             $image->crop($width, $height);
             $image->save(public_path() . $assetPath);
         }
         return URL::asset($assetPath);
     });
 }
Ejemplo n.º 8
0
 public function editarImagem($id)
 {
     if ($this->filtraInt($id)) {
         $diretorio = "upload/";
         move_uploaded_file($_FILES['imagem']["tmp_name"], $diretorio . $_FILES['imagem']["name"]);
         $resize = new ImageResize($diretorio . $_FILES['imagem']["name"]);
         $resize->crop(240, 320);
         unlink($diretorio . $_FILES['imagem']["name"]);
         $resize->save($diretorio . $_FILES['imagem']["name"]);
         $this->pessoa->setImagem($diretorio . $_FILES['imagem']["name"]);
         $this->pessoa->setId($id);
         $p = $this->pessoa->editar($this->pessoa);
         if ($p) {
             $this->redirecionar("matricula");
         }
     }
     $this->redirecionar("dashboard");
 }
Ejemplo n.º 9
0
 /**
  * @param  string $file
  * @param  int $width
  * @param  int $height
  * @return void
  */
 public function resizeAndSave($file, $width, $height)
 {
     $image = new ImageResize($_SERVER['DOCUMENT_ROOT'] . $file);
     if ($width == 'auto') {
         $image->resizeToHeight($height);
     } elseif ($height == 'auto') {
         $image->resizeToWidth($width);
     } else {
         $image->crop($width, $height);
     }
     unlink($_SERVER['DOCUMENT_ROOT'] . $file);
     $image->save($_SERVER['DOCUMENT_ROOT'] . $file);
 }