%PDF- %PDF-
| Direktori : /home/cargonizer/domains/cargonizer.com.tr/private_html/includes/ |
| Current File : /home/cargonizer/domains/cargonizer.com.tr/private_html/includes/thumbnail.php |
<?
class Thumbnail {
var $errmsg = "";
var $error = false;
var $format = "";
var $file = "";
var $max_width = 0;
var $max_height = 0;
var $percent = 0;
function Thumbnail($file, $max_width = 0, $max_height = 0, $percent = 0) {
if (!file_exists($file)) {
$this->errmsg = "Dosya yok";
$this->error = true;
}
else if (!is_readable($file)) {
$this->errmsg = "Dosya okunamiyor";
$this->error = true;
}
if (strstr(strtolower($file), ".gif"))
$this->format = "GIF";
else if (strstr(strtolower($file), ".jpg") ||
strstr(strtolower($file), ".jpeg"))
$this->format = "JPEG";
else if (strstr(strtolower($file), ".png"))
$this->format = "PNG";
else {
$this->errmsg = "Bilinmeyen dosya formati";
$this->error = true;
}
if ($max_width == 0 && $max_height == 0 && $percent == 0)
$percent = 100;
$this->max_width = $max_width;
$this->max_height = $max_height;
$this->percent = $percent;
$this->file = $file;
}
function calc_width($width, $height) {
$new_width = $this->max_width;
$new_wp = (100 * $new_width) / $width;
$new_height = ($height * $new_wp) / 100;
return array($new_width, $new_height);
}
function calc_height($width, $height) {
$new_height = $this->max_height;
$new_hp = (100 * $new_height) / $height;
$new_width = ($width * $new_hp) / 100;
return array($new_width, $new_height);
}
function calc_percent($width, $height) {
$new_width = ($width * $this->percent) / 100;
$new_height = ($height * $this->percent) / 100;
return array($new_width, $new_height);
}
function return_value($array) {
$array[0] = intval($array[0]);
$array[1] = intval($array[1]);
return $array;
}
function calc_image_size($width, $height) {
$new_size = array($width, $height);
if($this->percent > 0){
$new_size = $this->calc_percent($width, $height);
return $this->return_value($new_size);
}else{
$Ratio = ($height / $width);
if($width > $this->max_width || $height > $this->max_height) {
if(($width > $this->max_width) && ($this->max_width > 0)) {
$new_size[0] = $this->max_width;
$new_size[1] = $new_size[0] * $Ratio;
}else{
if($this->max_height > 0){
$new_size[0] = $width;
$new_size[1] = $height;
}
}
if($this->max_height > 0){
if ($new_size[1] > $this->max_height) {
$new_size[1] = $this->max_height;
$new_size[0] = $new_size[1] / $Ratio;
}
}
}
return $this->return_value($new_size);
}
}
function show_error_image() {
header("Content-type: image/png");
$err_img = imagecreate(90, 90);
$bg_color = imagecolorallocate($err_img, 0, 0, 0);
$fg_color1 = imagecolorallocate($err_img, 255, 255, 255);
$fg_color2 = imagecolorallocate($err_img, 255, 0, 0);
//imagestring($err_img, 3, 6, 6, "HATA:", $fg_color2);
//imagestring($err_img, 3, 55, 6, $this->errmsg, $fg_color1);
imagepng($err_img);
imagedestroy($err_img);
}
function show($name = "") {
if ($this->error) {
$this->show_error_image();
return;
}
$size = getimagesize($this->file);
$new_size = $this->calc_image_size($size[0], $size[1]);
#
# Good idea from Mariano Cano Pérez
# Requires GD 2.0.1 (PHP >= 4.0.6)
#
$new_image = imagecreatetruecolor($new_size[0], $new_size[1]);
sscanf("#ffffff", "#%2x%2x%2x", $red, $green, $blue);
$background_color = imagecolorallocate($new_image, $red, $green, $blue);
imagefilledrectangle($new_image, 0, 0, $new_size[0], $new_size[1], $background_color);
switch ($this->format) {
case "GIF":
$old_image = imagecreatefromgif($this->file);
break;
case "JPEG":
$old_image = imagecreatefromjpeg($this->file);
break;
case "PNG":
$old_image = imagecreatefrompng($this->file);
break;
}
imagealphablending($old_image, false);
imagesavealpha($old_image, true);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_size[0], $new_size[1], $size[0], $size[1]);
switch ($this->format) {
case "GIF":
if (!empty($name)){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagegif($new_image, $name);
} else {
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
header("Content-type: image/gif");
imagegif($new_image);
}
break;
case "JPEG":
if (!empty($name)){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
sscanf("#ffffff", "#%2x%2x%2x", $red, $green, $blue);
$transparency = imagecolorallocate($new_image, $red, $green, $blue);
// make the transaparent areas transparent
for ($x = 0; $x < $new_size[0]; $x++) {
for ($y = 0; $y < $new_size[1]; $y++) {
// we test wether we have some transparency, in which case we will merge the colors
$pixel = imagecolorsforindex($new_image, imagecolorat($new_image, $x, $y));
if ($pixel['alpha'] == 127) {
// we have full transparency. we make the pixel transparent
imagesetpixel($new_image, $x, $y, $transparency);
} else if ($pixel['alpha'] > 0) {
// we have some transparency. we combine the color with the default color
$alpha = ($pixel['alpha'] / 127);
$pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
$pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
$pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
$color = imagecolorclosest($new_image, $pixel['red'], $pixel['green'], $pixel['blue']);
imagesetpixel($new_image, $x, $y, $color);
}
}
}
imagejpeg($new_image, $name);
}else {
//imagealphablending($new_image, false);
//imagesavealpha($new_image, true);
sscanf("#ffffff", "#%2x%2x%2x", $red, $green, $blue);
$transparency = imagecolorallocate($new_image, $red, $green, $blue);
for ($x = 0; $x < $new_size[0]; $x++) {
for ($y = 0; $y < $new_size[1]; $y++) {
$pixel = imagecolorsforindex($new_image, imagecolorat($new_image, $x, $y));
if ($pixel['alpha'] == 127) {
imagesetpixel($new_image, $x, $y, $transparency);
} else if ($pixel['alpha'] > 0) {
$alpha = ($pixel['alpha'] / 127);
$pixel['red'] = round(($pixel['red'] * (1 -$alpha) + $red * ($alpha)));
$pixel['green'] = round(($pixel['green'] * (1 -$alpha) + $green * ($alpha)));
$pixel['blue'] = round(($pixel['blue'] * (1 -$alpha) + $blue * ($alpha)));
$color = imagecolorclosest($new_image, $pixel['red'], $pixel['green'], $pixel['blue']);
imagesetpixel($new_image, $x, $y, $color);
}
}
}
header("Content-type: image/jpeg");
imagejpeg($new_image);
}
break;
case "PNG":
if (!empty($name)){
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagepng($new_image, $name);
}else {
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
header("Content-type: image/png");
imagepng($new_image);
}
break;
}
imagedestroy($new_image);
imagedestroy($old_image);
return;
}
function save($name) {
$this->show($name);
}
}
?>