
16/02/2008, 23:53
|
 | | | Fecha de Ingreso: junio-2006
Mensajes: 550
Antigüedad: 18 años, 9 meses Puntos: 7 | |
Re: Subir Imagen Continuación class.upload.php
Código:
// we have to make sure the extra bit is the right color, or transparent
if ($image_reflection_height + $this->image_reflection_space > 0) {
// use the background color if present
if (!empty($this->image_background_color)) {
sscanf($this->image_background_color, "#%2x%2x%2x", $red, $green, $blue);
$fill = imagecolorallocate($tmp, $red, $green, $blue);
} else {
$fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127);
}
// fill in from the edge of the extra bit
imagefill($tmp, round($this->image_dst_x / 2), $this->image_dst_y + $image_reflection_height + $this->image_reflection_space - 1, $fill);
}
// copy the reflection
for ($y = 0; $y < $image_reflection_height; $y++) {
for ($x = 0; $x < $this->image_dst_x; $x++) {
$pixel_b = imagecolorsforindex($tmp, imagecolorat($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space));
$pixel_o = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $this->image_dst_y - $y - 1 + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)));
$alpha_o = 1 - ($pixel_o['alpha'] / 127);
$alpha_b = 1 - ($pixel_b['alpha'] / 127);
$opacity = $alpha_o * $transparency / 100;
if ($opacity > 0) {
$red = round((($pixel_o['red'] * $opacity) + ($pixel_b['red'] ) * $alpha_b) / ($alpha_b + $opacity));
$green = round((($pixel_o['green'] * $opacity) + ($pixel_b['green']) * $alpha_b) / ($alpha_b + $opacity));
$blue = round((($pixel_o['blue'] * $opacity) + ($pixel_b['blue'] ) * $alpha_b) / ($alpha_b + $opacity));
$alpha = ($opacity + $alpha_b);
if ($alpha > 1) $alpha = 1;
$alpha = round((1 - $alpha) * 127);
$color = imagecolorallocatealpha($tmp, $red, $green, $blue, $alpha);
imagesetpixel($tmp, $x, $y + $this->image_dst_y + $this->image_reflection_space, $color);
}
}
if ($transparency > 0) $transparency = $transparency - ($this->image_reflection_opacity / $image_reflection_height);
}
// copy the resulting image into the destination image
$this->image_dst_y = $this->image_dst_y + $image_reflection_height + $this->image_reflection_space;
$image_dst = $this->imagetransfer($tmp, $image_dst);
}
// reduce the JPEG image to a set desired size
if (is_numeric($this->jpeg_size) && $this->jpeg_size > 0 && ($this->image_convert == 'jpeg' || $this->image_convert == 'jpg')) {
// inspired by: JPEGReducer class version 1, 25 November 2004, Author: Huda M ElMatsani, justhuda at netscape dot net
$this->log .= '- JPEG desired file size : ' . $this->jpeg_size . '<br />';
// calculate size of each image. 75%, 50%, and 25% quality
ob_start(); imagejpeg($image_dst,'',75); $buffer = ob_get_contents(); ob_end_clean();
$size75 = strlen($buffer);
ob_start(); imagejpeg($image_dst,'',50); $buffer = ob_get_contents(); ob_end_clean();
$size50 = strlen($buffer);
ob_start(); imagejpeg($image_dst,'',25); $buffer = ob_get_contents(); ob_end_clean();
$size25 = strlen($buffer);
// calculate gradient of size reduction by quality
$mgrad1 = 25 / ($size50-$size25);
$mgrad2 = 25 / ($size75-$size50);
$mgrad3 = 50 / ($size75-$size25);
$mgrad = ($mgrad1 + $mgrad2 + $mgrad3) / 3;
// result of approx. quality factor for expected size
$q_factor = round($mgrad * ($this->jpeg_size - $size50) + 50);
if ($q_factor<1) {
$this->jpeg_quality=1;
} elseif ($q_factor>100) {
$this->jpeg_quality=100;
} else {
$this->jpeg_quality=$q_factor;
}
$this->log .= ' JPEG quality factor set to ' . $this->jpeg_quality . '<br />';
}
// converts image from true color, and fix transparency if needed
$this->log .= '- converting...<br />';
switch($this->image_convert) {
case 'gif':
// if the image is true color, we convert it to a palette
if (imageistruecolor($image_dst)) {
$this->log .= ' true color to palette<br />';
// creates a black and white mask
$mask = array(array());
for ($x = 0; $x < $this->image_dst_x; $x++) {
for ($y = 0; $y < $this->image_dst_y; $y++) {
$pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
$mask[$x][$y] = $pixel['alpha'];
}
}
sscanf($this->image_default_color, "#%2x%2x%2x", $red, $green, $blue);
// first, we merge the image with the background color, so we know which colors we will have
for ($x = 0; $x < $this->image_dst_x; $x++) {
for ($y = 0; $y < $this->image_dst_y; $y++) {
if ($mask[$x][$y] > 0){
// we have some transparency. we combine the color with the default color
$pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y));
$alpha = ($mask[$x][$y] / 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 = imagecolorallocate($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']);
imagesetpixel($image_dst, $x, $y, $color);
}
}
}
// transfrom the true color image into palette, with it merged default color in
// we will have the best color possible, including the background
if (empty($this->image_background_color)) {
imagetruecolortopalette($image_dst, true, 255);
$transparency = imagecolorallocate($image_dst, 254, 1, 253);
imagecolortransparent($image_dst, $transparency);
// make the transparent areas transparent
for ($x = 0; $x < $this->image_dst_x; $x++) {
for ($y = 0; $y < $this->image_dst_y; $y++) {
// we test wether we have enough opacity to justify keeping the color
if ($mask[$x][$y] > 120) imagesetpixel($image_dst, $x, $y, $transparency);
}
}
}
unset($mask);
}
break;
case 'jpg':
case 'bmp':
// if the image doesn't support any transparency, then we merge it with the default color
$this->log .= ' fills in transparency with default color<br />';
sscanf($this->image_default_color, "#%2x%2x%2x", $red, $green, $blue);
|