本文整理汇总了PHP中imagick::cropImage方法的典型用法代码示例。如果您正苦于以下问题:PHP imagick::cropImage方法的具体用法?PHP imagick::cropImage怎么用?PHP imagick::cropImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imagick
的用法示例。
在下文中一共展示了imagick::cropImage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: imgCrop
/**
* Crop image
*
* @param string $path image file
* @param int $width crop width
* @param int $height crop height
* @param bool $x crop left offset
* @param bool $y crop top offset
* @param string $destformat image destination format
* @return string|false
* @author Dmitry (dio) Levashov
* @author Alexey Sukhotin
**/
protected function imgCrop($path, $width, $height, $x, $y, $destformat = null)
{
if (($s = @getimagesize($path)) == false) {
return false;
}
$result = false;
switch ($this->imgLib) {
case 'imagick':
try {
$img = new imagick($path);
} catch (Exception $e) {
return false;
}
$img->cropImage($width, $height, $x, $y);
$result = $img->writeImage($path);
return $result ? $path : false;
break;
case 'gd':
if ($s['mime'] == 'image/jpeg') {
$img = imagecreatefromjpeg($path);
} elseif ($s['mime'] == 'image/png') {
$img = imagecreatefrompng($path);
} elseif ($s['mime'] == 'image/gif') {
$img = imagecreatefromgif($path);
} elseif ($s['mime'] == 'image/xbm') {
$img = imagecreatefromxbm($path);
}
if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
if (!imagecopy($tmp, $img, 0, 0, $x, $y, $width, $height)) {
return false;
}
if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') {
$result = imagejpeg($tmp, $path, 100);
} else {
if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') {
$result = imagegif($tmp, $path, 7);
} else {
$result = imagepng($tmp, $path, 7);
}
}
imagedestroy($img);
imagedestroy($tmp);
return $result ? $path : false;
}
break;
}
return false;
}
示例2: imgCrop
/**
* Crop image
*
* @param string $path image file
* @param int $width crop width
* @param int $height crop height
* @param bool $x crop left offset
* @param bool $y crop top offset
* @param string $destformat image destination format
* @return string|false
* @author Dmitry (dio) Levashov
* @author Alexey Sukhotin
**/
protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
if (($s = @getimagesize($path)) == false) {
return false;
}
$result = false;
switch ($this->imgLib) {
case 'imagick':
try {
$img = new imagick($path);
} catch (Exception $e) {
return false;
}
$img->cropImage($width, $height, $x, $y);
$result = $img->writeImage($path);
return $result ? $path : false;
break;
case 'gd':
if ($s['mime'] == 'image/jpeg') {
$img = imagecreatefromjpeg($path);
} elseif ($s['mime'] == 'image/png') {
$img = imagecreatefrompng($path);
} elseif ($s['mime'] == 'image/gif') {
$img = imagecreatefromgif($path);
} elseif ($s['mime'] == 'image/xbm') {
$img = imagecreatefromxbm($path);
}
if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
$bgcolor = $this->options['tmbBgColor'];
if ($bgcolor == 'transparent') {
list($r, $g, $b) = array(0, 0, 255);
} else {
list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
}
$bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);
if ($bgcolor == 'transparent') {
$bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
}
imagefill($tmp, 0, 0, $bgcolor1);
$size_w = $width;
$size_h = $height;
if ($s[0] < $width || $s[1] < $height) {
$size_w = $s[0];
$size_h = $s[1];
}
if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
return false;
}
if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
$result = imagejpeg($tmp, $path, 100);
} else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
$result = imagegif($tmp, $path, 7);
} else {
$result = imagepng($tmp, $path, 7);
}
imagedestroy($img);
imagedestroy($tmp);
return $result ? $path : false;
}
break;
}
return false;
}
示例3: imgCrop
/**
* Crop image
*
* @param string $path image file
* @param int $width crop width
* @param int $height crop height
* @param bool $x crop left offset
* @param bool $y crop top offset
* @param string $destformat image destination format
* @return string|false
* @author Dmitry (dio) Levashov
* @author Alexey Sukhotin
**/
protected function imgCrop($path, $width, $height, $x, $y, $destformat = null)
{
if (($s = @getimagesize($path)) == false) {
return false;
}
$result = false;
switch ($this->imgLib) {
case 'imagick':
try {
$img = new imagick($path);
} catch (Exception $e) {
return false;
}
$img->cropImage($width, $height, $x, $y);
$result = $img->writeImage($path);
return $result ? $path : false;
break;
case 'gd':
$img = self::gdImageCreate($path, $s['mime']);
if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
self::gdImageBackground($tmp, $this->options['tmbBgColor']);
$size_w = $width;
$size_h = $height;
if ($s[0] < $width || $s[1] < $height) {
$size_w = $s[0];
$size_h = $s[1];
}
if (!imagecopy($tmp, $img, 0, 0, $x, $y, $size_w, $size_h)) {
return false;
}
$result = self::gdImage($tmp, $path, $destformat, $s['mime']);
imagedestroy($img);
imagedestroy($tmp);
return $result ? $path : false;
}
break;
}
return false;
}
示例4: resizeCropImg
private function resizeCropImg($pFolder, $pImg, $pW, $pH)
{
$im = new imagick();
$im->readImage($pFolder . $pImg);
$image = new stdClass();
$image->dimensions = $im->getImageGeometry();
$image->w = $image->dimensions['width'];
$image->h = $image->dimensions['height'];
$image->ratio = $image->w / $image->h;
if ($image->w / $pW < $image->h / $pH) {
$h = ceil($pH * $image->w / $pW);
$y = ($image->h - $pH * $image->w / $pW) / 2;
$im->cropImage($image->w, $h, 0, $y);
} else {
$w = ceil($pW * $image->h / $pH);
$x = ($image->w - $pW * $image->h / $pH) / 2;
$im->cropImage($w, $image->h, $x, 0);
}
$im->ThumbnailImage($pW, $pH, true);
if ($img->type === "PNG") {
$im->setImageCompressionQuality(55);
$im->setImageFormat('png');
} elseif ($img->type === "JPG" || $img->type === "JPEG") {
$im->setCompressionQuality(100);
$im->setImageFormat("jpg");
}
$im->writeImage($this->ad->url->folder . '/assets/' . $pImg);
$im->destroy();
}