Eventviva\ImageResize::save PHP Method

save() public method

Saves new image
public save ( string $filename, string $image_type = null, integer $quality = null, integer $permissions = null ) : static
$filename string
$image_type string
$quality integer
$permissions integer
return static
    public function save($filename, $image_type = null, $quality = null, $permissions = null)
    {
        $image_type = $image_type ?: $this->source_type;
        $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
        imageinterlace($dest_image, $this->interlace);
        switch ($image_type) {
            case IMAGETYPE_GIF:
                $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
                imagecolortransparent($dest_image, $background);
                imagefill($dest_image, 0, 0, $background);
                imagesavealpha($dest_image, true);
                break;
            case IMAGETYPE_JPEG:
                $background = imagecolorallocate($dest_image, 255, 255, 255);
                imagefilledrectangle($dest_image, 0, 0, $this->getDestWidth(), $this->getDestHeight(), $background);
                break;
            case IMAGETYPE_PNG:
                imagealphablending($dest_image, false);
                imagesavealpha($dest_image, true);
                break;
        }
        imagecopyresampled($dest_image, $this->source_image, $this->dest_x, $this->dest_y, $this->source_x, $this->source_y, $this->getDestWidth(), $this->getDestHeight(), $this->source_w, $this->source_h);
        switch ($image_type) {
            case IMAGETYPE_GIF:
                imagegif($dest_image, $filename);
                break;
            case IMAGETYPE_JPEG:
                if ($quality === null) {
                    $quality = $this->quality_jpg;
                }
                imagejpeg($dest_image, $filename, $quality);
                break;
            case IMAGETYPE_PNG:
                if ($quality === null) {
                    $quality = $this->quality_png;
                }
                imagepng($dest_image, $filename, $quality);
                break;
        }
        if ($permissions) {
            chmod($filename, $permissions);
        }
        imagedestroy($dest_image);
        return $this;
    }

Usage Example

コード例 #1
0
 /**
  * Resize an image and optionally rename it.
  *
  * @param string $path
  * @param string $filename
  * @param int $width
  * @param string|null $newFilename
  */
 public function resizeToWidth($path, $filename, $width, $newFilename = null)
 {
     $fullFilename = $path . '/' . $filename;
     $image = new ImageResize($fullFilename);
     $image->resizeToWidth($width, true);
     if (is_string($newFilename) && !empty($newFilename)) {
         $image->save($path . '/' . $newFilename);
         return;
     }
     $image->save($fullFilename);
 }
All Usage Examples Of Eventviva\ImageResize::save