/** * Generate thumbnail * * @throws Exception * @return string Path to new file */ public function generate() { $dir = $this->path . '/.thumb/' . $this->width . 'x' . $this->height; if (!is_dir($dir) && !mkdir($dir, 0755, true)) { throw new Exception("Thumbnail image can't be save. Parent directory is not writable"); } // Thumbnail already exists // then remove it and regenerate if (file_exists($dir . '/' . $this->file)) { unlink($dir . '/' . $this->file); } if (class_exists('\\Imagick')) { $image = new \Imagick($this->path . '/' . $this->file); } elseif (function_exists('gd_info')) { $image = new Gd($this->path . '/' . $this->file); } else { // return original file return $this->path . '/' . $this->file; } $image->cropThumbnailImage($this->width, $this->height); $image->writeimage($dir . '/' . $this->file); return $dir . '/' . $this->file; }