Codice: Seleziona tutto
<?php
class Image
{
private $check_ext;
private $max_width, $max_height, $max_filesize;
private $img;
private $infos;
private $q, $lo_q;
function Image($img)
{
$this->img = $img;
$this->check_ext = array("jpg", "jpe", "jpeg", "gif", "png");
$this->get_infos();
}
function set_max_height($mh)
{
$this->max_height = $mh;
}
function no_max_height()
{
$this->max_height = 0;
}
function set_max_width($mw)
{
$this->max_width = $mw;
}
function no_max_width()
{
$this->max_width = 0;
}
function set_max_filesize($ms)
{
$this->max_filesize = $ms;
}
function no_max_filesize()
{
$this->max_filesize = 0;
}
function ext_accept($extensions) // str
{
$this->check_ext = explode(",", $extensions);
}
function get_extension()
{
$filename = basename($this->img);
$ext = substr($filename, strrpos($filename, "."), strlen($filename));
return $ext;
}
function get_content_type()
{
if(empty($this->infos))
{
$this->get_infos();
}
if($this->infos['width']!=0 && $this->infos['height']!=0)
{
return image_type_to_mime_type($this->infos['type']);
}
else
{
return false;
}
}
function get_infos()
{
list($this->infos['width'], $this->infos['height'], $this->infos['type']) = getimagesize($this->img);
$this->max_width = 0;
$this->max_height = 0;
$this->min_width = 10;
$this->q = 100;
$this->lo_q = 30;
}
function get_new_dimensions($width, $height, $max_width, $max_height = 0)
{
$new_width = $width;
$new_height = $height;
if($max_height!=0 && $max_width==0)
{
if($height > $max_height)
{
$new_height = $max_height;
$new_width = round($width * $max_height / $height, 1);
}
}
else if($max_width!=0 && $max_height==0)
{
if($width > $max_width)
{
$new_width = $max_width;
$new_height = round($height * $max_width / $width, 1);
}
}
else
{
if($max_height!=0 && $max_width!=0)
{
if($width >= $height
&& $width >= $max_width)
{
$new_width = $max_width;
$new_height = round($height * $new_width / $width, 1);
if($new_height > $max_height)
{
$new_height2 = $max_height;
$new_width2 = round($new_width * $new_height2 / $new_height, 1);
$new_height = $new_height2;
$new_width = $new_width2;
}
}
else if($height >= $width
&& $height >= $max_height)
{
$new_height = $max_height;
$new_width = round($width * $new_height / $height, 1);
if($new_width > $max_width)
{
$new_width2 = $max_width;
$new_height2 = round($new_height * $new_width2 / $new_width, 1);
$new_width = $new_width2;
$new_height = $new_height2;
}
}
}
}
return array($new_width, $new_height);
}
function create_thumb($source, $w_source, $h_source, $th_width, $th_height)
{
$thumb = $this->generate_thumb($source, $w_source, $h_source, $th_width, $th_height);
// Reduce filesize
if($this->max_filesize)
{
$thumb = $this->reduce_filesize($source, $thumb, $w_source, $h_source, $th_width, $th_height);
}
return $thumb;
}
function generate_thumb($source, $w_source, $h_source, $th_width, $th_height)
{
$thumb = imagecreatetruecolor($th_width, $th_height);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $th_width, $th_height, $w_source, $h_source);
return $thumb;
}
function get_filesize($img, $remote = 0)
{
if($remote)
{
// Not available yet
return 0;
}
else
{
return filesize($img);
}
}
function get_thumb_filesize($thumb)
{
$img = md5(time());
switch($this->get_content_type())
{
case "image/jpeg":
imagejpeg($thumb, $img, $this->q);
break;
case "image/gif";
imagegif($thumb, $img);
break;
case "image/png";
imagepng($thumb, $img);
break;
}
if(file_exists($img))
{
$fs = $this->get_filesize($img);
unlink($img);
return $fs;
}
else
{
return 0;
}
}
function reduce_filesize($source, $thumb, $w_source, $h_source, $th_width, $th_height)
{
$img = $this->img;
switch($this->get_content_type())
{
case "image/jpeg":
$tmp_imgs = array();
do
{
$img = md5($img);
$tmp_imgs[] = $img;
imagejpeg($thumb, $img, $this->q);
$this->q--;
}
while($this->get_filesize($img)>$this->max_filesize && $this->q>$this->lo_q);
$last_filesize = $this->get_filesize($img);
foreach($tmp_imgs as $ti)
{
unlink($ti);
}
// After reducing image quality, the filesize is still bigger than the limit
if($last_filesize>$this->max_filesize)
{
return $this->reduce_dimensions_by_filesize($source, $w_source, $h_source, $last_filesize);
}
else
{
return $thumb;
}
break;
case "image/gif";
return $thumb;
break;
case "image/png";
return $thumb;
break;
}
}
function reduce_dimensions_by_filesize($source, $w_source, $h_source, $filesize)
{
$max_width = $this->max_width;
while($filesize>$this->max_filesize && $max_width>$this->min_width)
{
$max_width -= 5;
list($new_width, $new_height) = $this->get_new_dimensions($w_source, $h_source, $max_width, 0);
$thumb = $this->generate_thumb($source, $w_source, $h_source, $new_width, $new_height);
$filesize = $this->get_thumb_filesize($thumb);
}
return $thumb;
}
function resize($save_to_source = 1)
{
list($new_width, $new_height) = $this->get_new_dimensions($this->infos['width'], $this->infos['height'], $this->max_width, $this->max_height);
if($new_height!=$this->infos['height'] || $new_width!=$this->infos['width'])
{
switch($this->get_content_type())
{
case "image/jpeg":
$source = imagecreatefromjpeg($this->img);
$thumb = $this->create_thumb($source, $this->infos['width'], $this->infos['height'], $new_width, $new_height);
if($save_to_source)
{
imagejpeg($thumb, $this->img, $this->q);
return true;
}
else
{
header("Content-Type: ".$this->get_content_type());
imagejpeg($thumb, null, $this->q);
}
break;
case "image/gif":
$source = imagecreatefromgif($this->img);
$thumb = $this->create_thumb($source, $this->infos['width'], $this->infos['height'], $new_width, $new_height);
if($save_to_source)
{
imagegif($thumb, $this->img);
return true;
}
else
{
header("Content-Type: ".$this->get_content_type());
imagegif($thumb);
}
break;
case "image/png":
$source = imagecreatefrompng($this->img);
$thumb = $this->create_thumb($source, $this->infos['width'], $this->infos['height'], $new_width, $new_height);
if($save_to_source)
{
imagepng($thumb, $this->img);
return true;
}
else
{
header("Content-Type: ".$this->get_content_type());
imagepng($thumb);
}
break;
default:
// Do nothing
return false;
}
}
}
}
?>