Gc\Media\Image::resize PHP Méthode

resize() public méthode

Resize image
public resize ( integer $newWidth, integer $newHeight, string $option = 'auto', string $backgroundColor = '#000000', integer $sourceX, integer $sourceY ) : Image
$newWidth integer New width
$newHeight integer New height
$option string Option can be (auto|crop)
$backgroundColor string Background color
$sourceX integer Source x
$sourceY integer Source y
Résultat Image
    public function resize($newWidth, $newHeight, $option = 'auto', $backgroundColor = '#000000', $sourceX = 0, $sourceY = 0)
    {
        if (empty($this->image)) {
            return false;
        }
        if (!in_array($option, $this->availableOptions)) {
            $option = 'auto';
        }
        if ($option == 'crop') {
            $optimalwidth = $newWidth;
            $optimalheight = $newHeight;
            if ($this->width < $newWidth) {
                $optimalwidth = $this->width;
            }
            if ($this->height < $newHeight) {
                $optimalheight = $this->height;
            }
            $this->crop($optimalwidth, $optimalheight, $sourceX, $sourceY);
        } else {
            $optimalheight = $this->getSizeByFixedWidth($newWidth);
            $optimalwidth = $this->getSizeByFixedHeight($newHeight);
            if ($optimalheight > $newHeight) {
                $optimalheight = $this->getSizeByFixedWidth($optimalwidth);
            } elseif ($optimalwidth > $newWidth) {
                $optimalwidth = $this->getSizeByFixedHeight($optimalheight);
            }
            $this->imageResized = imagecreatetruecolor($optimalwidth, $optimalheight);
            imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalwidth, $optimalheight, $this->width, $this->height);
        }
        $tmpimage = @imagecreatetruecolor($newWidth, $newHeight);
        $rgbArray = $this->hex2rgb($backgroundColor);
        if (empty($rgbArray)) {
            $backgroundColor = imagecolorallocate($tmpimage, 0, 0, 0);
        } else {
            $backgroundColor = imagecolorallocate($tmpimage, $rgbArray['red'], $rgbArray['green'], $rgbArray['blue']);
        }
        imagefill($tmpimage, 0, 0, $backgroundColor);
        $dstX = (int) (0.5 * ($newWidth - $optimalwidth));
        $dstY = (int) (0.5 * ($newHeight - $optimalheight));
        imagecopyresampled($tmpimage, $this->imageResized, $dstX, $dstY, 0, 0, $optimalwidth, $optimalheight, $optimalwidth, $optimalheight);
        $this->imageResized = $tmpimage;
        return $this;
    }

Usage Example

Exemple #1
0
 /**
  * Save Image cropper editor
  *
  * @return void
  */
 public function save()
 {
     $post = $this->getRequest()->getPost();
     $parameters = $this->getConfig();
     $data = array();
     $imageModel = new Image();
     $fileClass = new File();
     $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument(), $this->getName());
     $backgroundColor = empty($parameters['background']) ? '#000000' : $parameters['background'];
     if (!empty($_FILES[$this->getName()]['name'])) {
         $oldFiles = $_FILES;
         $file = $_FILES[$this->getName()];
         //Ignore others data
         $_FILES = array();
         $_FILES[$this->getName()] = $file;
         $fileClass->upload();
         $files = $fileClass->getFiles();
         if (!empty($files) and is_array($files)) {
             foreach ($files as $file) {
                 $name = $file['filename'];
                 $file = $fileClass->getPath() . $name;
                 if (file_exists($file)) {
                     $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
                     $finfo = finfo_open($const);
                     // return mimetype extension
                     if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) {
                         unlink($file);
                     } else {
                         $fileInfo = @getimagesize($file);
                         $data['original'] = array('value' => $name, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']);
                         $imageModel->open($file);
                         foreach ($parameters['size'] as $size) {
                             $imageModel->open($file);
                             $imageModel->resize($size['width'], $size['height'], empty($parameters['resize_option']) ? 'auto' : $parameters['resize_option'], $backgroundColor);
                             $sizeFilename = preg_replace('~\\.([a-zA-Z]+)$~', '-' . $size['name'] . '.$1', $name);
                             $imageModel->save($fileClass->getPath() . $sizeFilename);
                             $fileInfo = @getimagesize($fileClass->getPath() . $sizeFilename);
                             $data[$size['name']] = array('value' => $sizeFilename, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime'], 'x' => 0, 'y' => 0);
                         }
                     }
                     finfo_close($finfo);
                 }
             }
         }
         //Restore file data
         $_FILES = $oldFiles;
     } else {
         $data = $post->get($this->getName() . '-hidden');
         $data = unserialize($data);
         if (!empty($data)) {
             if (!empty($data['original']['value'])) {
                 foreach ($parameters['size'] as $size) {
                     $x = (int) $post->get($this->getName() . $size['name'] . '-x');
                     $y = (int) $post->get($this->getName() . $size['name'] . '-y');
                     $filename = !empty($data[$size['name']]['value']) ? $data[$size['name']]['value'] : preg_replace('~\\.([a-zA-Z]+)$~', '-' . $size['name'] . '.$1', $data['original']['value']);
                     $imageModel->open($fileClass->getPath() . $data['original']['value']);
                     $imageModel->resize($size['width'], $size['height'], empty($parameters['resize_option']) ? 'auto' : $parameters['resize_option'], $backgroundColor, $x, $y);
                     $imageModel->save($fileClass->getPath() . $filename);
                     if (!empty($data[$size['name']]['value'])) {
                         $data[$size['name']]['x'] = $x;
                         $data[$size['name']]['y'] = $y;
                     } else {
                         $fileInfo = @getimagesize($fileClass->getPath() . $filename);
                         $data[$size['name']] = array('value' => $filename, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime'], 'x' => 0, 'y' => 0);
                     }
                 }
                 foreach ($data as $name => $file) {
                     if ($name == 'original') {
                         continue;
                     }
                     $found = false;
                     foreach ($parameters['size'] as $size) {
                         if ($size['name'] == $name) {
                             $found = true;
                             $file['options'] = $size;
                             break;
                         }
                     }
                     if (empty($found)) {
                         unset($data[$name]);
                     }
                 }
             }
         }
     }
     $data = serialize($data);
     $this->setValue(empty($data) ? '' : $data);
 }