If no height is given, the width will be used as a height, thus creating a square crop
public cropFromCenter ( integer $cropWidth, integer $cropHeight = null ) : GdThumb | ||
$cropWidth | integer | |
$cropHeight | integer | |
Résultat | GdThumb |
public function zoom($width, $height = null) { return parent::cropFromCenter($width, $height); }
public static function createThumb($source, $dest = null, $width = 200, $height = 160, $mode = 'resize') { if (!class_exists('ThumbBase')) { require_once JPATH_SITE . '/plugins/system/jvhelper/libs/ThumbBase.inc.php'; } if (!class_exists('GdThumb')) { require_once JPATH_SITE . '/plugins/system/jvhelper/libs/GdThumb.inc.php'; } if (!$dest) { $dest = JPATH_SITE . '/images/jvthumb'; } $size = @getimagesize($source); if (!is_array($size)) { return $source; } $replace = JURI::base(true) . '/'; if (strpos($source, $replace) === 0) { $source = substr($source, strlen($replace)); } if (preg_match('#http(s?):\\/\\/#', $source) && $source) { if (stripos($size['mime'], 'image') === 0) { $extension = substr($size['mime'], 6); if ($extension == 'jpeg') { $extension = 'jpg'; } $info = array('extension' => $extension, 'filename' => substr(base64_encode(json_encode($size)), 0, 10)); } else { return $source; } } else { $source = JPATH_SITE . '/' . $source; $info = pathinfo($source); } // Resize path $path = $dest . "/{$mode}_{$width}_{$height}_{$info['filename']}.{$info['extension']}"; if (!is_dir(dirname($path))) { JFolder::create(dirname($path)); $index = "<html><body></body></html>"; if (!JFile::write($path . '/' . 'index.html', $index)) { return; } } if (!is_file($path)) { // Resize $thumb = new GdThumb($source, array('jpegQuality' => 80)); if ($mode == 'resize') { $thumb->resize($width, $height); } if ($mode == 'crop') { $thumb->cropFromCenter($width, $height); } $thumb->save($path, $info['extension']); } return self::toURL($path); }
public static function createThumb($source, $dest, $width, $height, $mode) { $size = @getimagesize($source); if (!is_array($size)) { return $source; } $replace = JURI::base(true) . '/'; if (strpos($source, $replace) === 0) { $source = substr($source, strlen($replace)); } if (preg_match('#http(s?):\\/\\/#', $source) && $source) { if (stripos($size['mime'], 'image') === 0) { $extension = substr($size['mime'], 6); if ($extension == 'jpeg') { $extension = 'jpg'; } $info = array('extension' => $extension, 'filename' => substr(base64_encode(json_encode($size)), 0, 10)); } else { return $source; } } else { $source = JPATH_SITE . '/' . $source; $info = pathinfo($source); } // Resize path $path = $dest . "/{$mode}_{$width}_{$info['filename']}.{$info['extension']}"; if (!is_dir(dirname($path))) { JFolder::create(dirname($path)); $index = "<html><body></body></html>"; if (!JFile::write($path . '/' . 'index.html', $index)) { return; } } if (!is_file($path)) { // Resize if ($mode == 'original') { JFile::copy($source, $path); return self::toURL($path); } $thumb = new GdThumb($source, array('jpegQuality' => 90)); if ($mode == 'resize') { $thumb->resize($width, $height); } if ($mode == 'crop') { $thumb->cropFromCenter($width, $height); } $thumb->save($path, $info['extension']); } return self::toURL($path); }