public function convert($type)
{
$type = strtolower($type);
// Check if the requested image type is supported.
if (!array_key_exists($type, $this->allowed)) {
throw new Exception('Error: That image type is not supported. Only GIF, JPG and PNG image types are supported.');
// Check if the image is already the requested image type.
} else {
if (strtolower($this->ext) == $type) {
throw new Exception('Error: This image file is already a ' . strtoupper($type) . ' image file.');
}
}
// Else, save the image as the new type.
// Open a new image, maintaining the GIF image's palette and
// transparency where applicable.
if ($this->mime == 'image/gif') {
$this->createResource();
imageinterlace($this->resource, 0);
// Change the type of the image object to the new,
// requested image type.
$this->ext = $type;
$this->mime = $this->allowed[$this->ext];
// Redefine the image object properties with the new values.
$this->fullpath = $this->dir . $this->filename . '.' . $this->ext;
$this->basename = basename($this->fullpath);
// Else, open a new true color image.
} else {
if ($type == 'gif') {
$this->createResource();
// Change the type of the image object to the new,
// requested image type.
$this->ext = $type;
$this->mime = $this->allowed[$this->ext];
// Redefine the image object properties with the new values.
$this->fullpath = $this->dir . $this->filename . '.' . $this->ext;
$this->basename = basename($this->fullpath);
} else {
$new = imagecreatetruecolor($this->width, $this->height);
// Create a new, blank image file and copy the image over.
$this->createResource();
// Change the type of the image object to the new,
// requested image type.
$this->ext = $type;
$this->mime = $this->allowed[$this->ext];
// Redefine the image object properties with the new values.
$this->fullpath = $this->dir . $this->filename . '.' . $this->ext;
$this->basename = basename($this->fullpath);
// Create and save the image in it's new, proper format.
imagecopyresampled($new, $this->resource, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
}
}
return $this;
}