Add new common Image::scale() method
- Fix undefined variable $type
This commit is contained in:
parent
910d3f7b5f
commit
a6a2c578f9
1 changed files with 53 additions and 72 deletions
|
@ -142,7 +142,7 @@ class Image
|
||||||
* Setup the image to the format it will be saved to
|
* Setup the image to the format it will be saved to
|
||||||
*/
|
*/
|
||||||
$map = self::getFormatsMap();
|
$map = self::getFormatsMap();
|
||||||
$format = $map[$type];
|
$format = $map[$this->type];
|
||||||
$this->image->setFormat($format);
|
$this->image->setFormat($format);
|
||||||
|
|
||||||
// Always coalesce, if it is not a multi-frame image it won't hurt anyway
|
// Always coalesce, if it is not a multi-frame image it won't hurt anyway
|
||||||
|
@ -338,42 +338,7 @@ class Image
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->scale($dest_width, $dest_height);
|
||||||
if ($this->isImagick()) {
|
|
||||||
/*
|
|
||||||
* If it is not animated, there will be only one iteration here,
|
|
||||||
* so don't bother checking
|
|
||||||
*/
|
|
||||||
// Don't forget to go back to the first frame
|
|
||||||
$this->image->setFirstIterator();
|
|
||||||
do {
|
|
||||||
// FIXME - implement horizantal bias for scaling as in followin GD functions
|
|
||||||
// to allow very tall images to be constrained only horizontally.
|
|
||||||
|
|
||||||
$this->image->scaleImage($dest_width, $dest_height);
|
|
||||||
} while ($this->image->nextImage());
|
|
||||||
|
|
||||||
// These may not be necessary any more
|
|
||||||
$this->width = $this->image->getImageWidth();
|
|
||||||
$this->height = $this->image->getImageHeight();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$dest = imagecreatetruecolor($dest_width, $dest_height);
|
|
||||||
imagealphablending($dest, false);
|
|
||||||
imagesavealpha($dest, true);
|
|
||||||
if ($this->type=='image/png') {
|
|
||||||
imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
|
|
||||||
}
|
|
||||||
imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
|
|
||||||
if ($this->image) {
|
|
||||||
imagedestroy($this->image);
|
|
||||||
}
|
|
||||||
$this->image = $dest;
|
|
||||||
$this->width = imagesx($this->image);
|
|
||||||
$this->height = imagesy($this->image);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -562,23 +527,7 @@ class Image
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isImagick()) {
|
return $this->scale($dest_width, $dest_height);
|
||||||
return $this->scaleDown($dest_width, $dest_height);
|
|
||||||
}
|
|
||||||
|
|
||||||
$dest = imagecreatetruecolor($dest_width, $dest_height);
|
|
||||||
imagealphablending($dest, false);
|
|
||||||
imagesavealpha($dest, true);
|
|
||||||
if ($this->type=='image/png') {
|
|
||||||
imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
|
|
||||||
}
|
|
||||||
imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
|
|
||||||
if ($this->image) {
|
|
||||||
imagedestroy($this->image);
|
|
||||||
}
|
|
||||||
$this->image = $dest;
|
|
||||||
$this->width = imagesx($this->image);
|
|
||||||
$this->height = imagesy($this->image);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -591,27 +540,59 @@ class Image
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->isImagick()) {
|
return $this->scale($dim, $dim);
|
||||||
$this->image->setFirstIterator();
|
}
|
||||||
do {
|
|
||||||
$this->image->scaleImage($dim, $dim);
|
/**
|
||||||
} while ($this->image->nextImage());
|
* @brief Scale image to target dimensions
|
||||||
return;
|
*
|
||||||
|
* @param int $dest_width
|
||||||
|
* @param int $dest_height
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function scale($dest_width, $dest_height)
|
||||||
|
{
|
||||||
|
if (!$this->isValid()) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$dest = imagecreatetruecolor($dim, $dim);
|
if ($this->isImagick()) {
|
||||||
imagealphablending($dest, false);
|
/*
|
||||||
imagesavealpha($dest, true);
|
* If it is not animated, there will be only one iteration here,
|
||||||
if ($this->type=='image/png') {
|
* so don't bother checking
|
||||||
imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
|
*/
|
||||||
|
// Don't forget to go back to the first frame
|
||||||
|
$this->image->setFirstIterator();
|
||||||
|
do {
|
||||||
|
// FIXME - implement horizontal bias for scaling as in following GD functions
|
||||||
|
// to allow very tall images to be constrained only horizontally.
|
||||||
|
$this->image->scaleImage($dest_width, $dest_height);
|
||||||
|
} while ($this->image->nextImage());
|
||||||
|
|
||||||
|
// These may not be necessary anymore
|
||||||
|
$this->width = $this->image->getImageWidth();
|
||||||
|
$this->height = $this->image->getImageHeight();
|
||||||
|
} else {
|
||||||
|
$dest = imagecreatetruecolor($dest_width, $dest_height);
|
||||||
|
imagealphablending($dest, false);
|
||||||
|
imagesavealpha($dest, true);
|
||||||
|
|
||||||
|
if ($this->type=='image/png') {
|
||||||
|
imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
|
||||||
|
}
|
||||||
|
|
||||||
|
imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $this->width, $this->height);
|
||||||
|
|
||||||
|
if ($this->image) {
|
||||||
|
imagedestroy($this->image);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->image = $dest;
|
||||||
|
$this->width = imagesx($this->image);
|
||||||
|
$this->height = imagesy($this->image);
|
||||||
}
|
}
|
||||||
imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
|
|
||||||
if ($this->image) {
|
return true;
|
||||||
imagedestroy($this->image);
|
|
||||||
}
|
|
||||||
$this->image = $dest;
|
|
||||||
$this->width = imagesx($this->image);
|
|
||||||
$this->height = imagesy($this->image);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue