/** * sharpen( $factor ) - factor goes from -64 to 64 where -64 is max blur and 64 is max sharpen **/ public function sharpenBlur($factor) { if ($factor == 0) { return $this; } // get a value thats equal to 64 - abs( factor ) // ( using min/max to limited the factor to 0 - 64 to not get out of range values ) $val1Adjustment = 64 - min(64, max(0, abs($factor))); // the base factor for the "current" pixel depends on if we are blurring or sharpening. // If we are blurring use 1, if sharpening use 9. $val1Base = abs($factor) != $factor ? 1 : 9; // value for the center/currrent pixel is: // 1 + 0 - max blurring // 1 + 64- minimal blurring // 9 + 64- minimal sharpening // 9 + 0 - maximum sharpening $val1 = $val1Base + $val1Adjustment; // the value for the surrounding pixels is either positive or negative depending on if we are blurring or sharpening. $val2 = abs($factor) != $factor ? 1 : -1; // setup matrix .. $matrix = array(array($val2, $val2, $val2), array($val2, $val1, $val2), array($val2, $val2, $val2)); // calculate the correct divisor // actual divisor is equal to "$divisor = $val1 + $val2 * 8;" // but the following line is more generic $divisor = array_sum(array_map('array_sum', $matrix)); // apply the matrix imageconvolution($this->image, $matrix, $divisor, 0); return $this; }
public static function resize($file_name, $file_type) { switch ($file_type) { // узнаем тип картинки case "image/gif": $im = imagecreatefromgif($file_name); break; case "image/jpeg": $im = imagecreatefromjpeg($file_name); break; case "image/png": $im = imagecreatefrompng($file_name); break; case "image/pjpeg": $im = imagecreatefromjpeg($file_name); break; } list($w, $h) = getimagesize($file_name); // берем высоту и ширину $koe = $w / 600; // вычисляем коэффициент 600 это ширина которая должна быть $new_h = ceil($h / $koe); // с помощью коэффициента вычисляем высоту $im1 = imagecreatetruecolor(600, $new_h); // создаем картинку imagecopyresampled($im1, $im, 0, 0, 0, 0, 600, $new_h, imagesx($im), imagesy($im)); imageconvolution($im1, array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0); imagejpeg($im1, $file_name, 100); // переводим в jpg imagedestroy($im); imagedestroy($im1); }
function image($mot) { $size = 40; $marge = 5; $box = imagettfbbox($size, 0, 'police/actionj.ttf', $mot); $largeur = $box[2] - $box[0]; $hauteur = $box[1] - $box[7]; $largeur_lettre = round($largeur / strlen($mot)); $font = 'police/actionj.ttf'; // Flou Gaussien $matrix_blur = array(array(1, 1, 1), array(1, 1, 1), array(1, 1, 1)); $img = imagecreate($largeur + $marge * 2, $hauteur + $marge * 2); $blanc = imagecolorallocate($img, 255, 255, 255); $noir = imagecolorallocate($img, 0, 0, 0); $special_couleur = array(imagecolorallocate($img, 30, 140, 130), imagecolorallocate($img, 200, 14, 130), imagecolorallocate($img, 30, 140, 13), imagecolorallocate($img, 0, 140, 208), imagecolorallocate($img, 80, 180, 130), imagecolorallocate($img, 30, 255, 0)); for ($i = 0; $i < strlen($mot); $i++) { $l = $mot[$i]; $angle = mt_rand(-35, 35); //On génère un angle entre -35 et 35 degrés //On écrit et on place le texte dans l'image imagettftext($img, $size, $angle, $i * $largeur_lettre + $marge, $hauteur + mt_rand(0, $marge / 2), $special_couleur[array_rand($special_couleur)], $font, $l); } imageline($img, 2, mt_rand(2, $hauteur), $largeur + $marge, mt_rand(2, $hauteur), $noir); //On réalise des barres aléatoires imageline($img, 2, mt_rand(2, $hauteur), $largeur + $marge, mt_rand(2, $hauteur), $noir); imageconvolution($img, $matrix_blur, 9, 0); // On applique la matrice, avec un diviseur 9 imageconvolution($img, $matrix_blur, 9, 0); imagepng($img); //affiche l'image au format png imagedestroy($img); //libère toute la mémoire associé à l'image }
function gaussianBlur($gd, $direction = 90, $speed = 1.0, $bidirectional = TRUE) { $map = array(array(1, 0), array(2, 0), array(2, 1), array(2, 2), array(1, 2), array(0, 2), array(0, 1), array(0, 0)); // Convert the 360 degree direction to a 0-7 direction index (float) $direction %= 360; $direction /= 45; // Create an empty convolution matrix $matrix = array_fill(0, 3, array(0.0, 0.0, 0.0)); $matrix[1][1] = 1.0; // Find the starting index (the counter-clockwise side) $index = (int) $direction; // Calculate how much of the speed should be passed to the next direction $spread = fmod($direction, 1.0); // Counter-clockwise side gets the inverse of $spread list($x, $y) = $map[$index]; $matrix[$x][$y] = $speed * (1.0 - $spread); if ($bidirectional) { // Apply the same 180 degrees opposed list($x, $y) = $map[($index + 4) % 8]; $matrix[$x][$y] = $speed * (1.0 - $spread); } // Clockwise side gets the rest from $spread list($x, $y) = $map[($index + 1) % 8]; $matrix[$x][$y] = $speed * $spread; if ($bidirectional) { // Apply the same 180 degrees opposed list($x, $y) = $map[($index + 5) % 8]; $matrix[$x][$y] = $speed * $spread; } $divisor = array_sum(array_map('array_sum', $matrix)); imageconvolution($gd, $matrix, $divisor, 0); return $gd; }
function captcha($mot) { $size = 64; $marge = 15; $font = '../fonts/angelina.ttf'; $matrix_blur = array(array(1, 1, 1), array(1, 1, 1), array(1, 1, 1)); $box = imagettfbbox($size, 0, $font, $mot); $largeur = $box[2] - $box[0]; $hauteur = $box[1] - $box[7]; $largeur_lettre = round($largeur / strlen($mot)); $img = imagecreate($largeur + $marge, $hauteur + $marge); $blanc = imagecolorallocate($img, 255, 255, 255); $noir = imagecolorallocate($img, 0, 0, 0); $couleur = array(imagecolorallocate($img, 0x99, 0x0, 0x66), imagecolorallocate($img, 0xcc, 0x0, 0x0), imagecolorallocate($img, 0x0, 0x0, 0xcc), imagecolorallocate($img, 0x0, 0x0, 0xcc), imagecolorallocate($img, 0xbb, 0x88, 0x77)); for ($i = 0; $i < strlen($mot); ++$i) { $l = $mot[$i]; $angle = mt_rand(-35, 35); imagettftext($img, mt_rand($size - 7, $size), $angle, $i * $largeur_lettre + $marge, $hauteur + mt_rand(0, $marge / 2), $couleur[array_rand($couleur)], $font, $l); } imageline($img, 2, mt_rand(2, $hauteur), $largeur + $marge, mt_rand(2, $hauteur), $noir); imageline($img, 2, mt_rand(2, $hauteur), $largeur + $marge, mt_rand(2, $hauteur), $noir); imageconvolution($img, $matrix_blur, 10, 10); imageconvolution($img, $matrix_blur, 10, 0); imagepng($img); imagedestroy($img); }
/** * Show the captcha image. * * @param integer $iRandom * @return void */ public function show($iRandom = null) { if (!empty($iRandom)) { $this->_sStr = Various::genRnd($iRandom, 5); } else { $this->_sStr = Various::genRnd('pH7_Pierre-Henry_Soria_Sanz_González_captcha', 5); } $this->_oSession->set('rand_code', $this->_sStr); $this->_sFont = $this->_getFont(); //$sBackground = PH7_PATH_DATA . 'background/' . mt_rand(1, 5) . '.png'; $this->_aBox = imagettfbbox($this->_iSize, 0, $this->_sFont, $this->_sStr); $this->_iWidth = $this->_aBox[2] - $this->_aBox[0]; $this->_iHeight = $this->_aBox[1] - $this->_aBox[7]; unset($this->_aBox); $this->_iStringWidth = round($this->_iWidth / strlen($this->_sStr)); //$this->_rImg = imagecreatefrompng($sBackground); $this->_rImg = imagecreate($this->_iWidth + $this->_iMargin, $this->_iHeight + $this->_iMargin); $this->_aColor = array(imagecolorallocate($this->_rImg, 0x99, 0x0, 0x66), imagecolorallocate($this->_rImg, 0xcc, 0x0, 0x0), imagecolorallocate($this->_rImg, 0x0, 0x0, 0xcc), imagecolorallocate($this->_rImg, 0x0, 0x0, 0xcc), imagecolorallocate($this->_rImg, 0xbb, 0x88, 0x77)); $this->_rBlack = imagecolorallocate($this->_rImg, 0, 0, 0); $this->_rRed = imagecolorallocate($this->_rImg, 200, 100, 90); $this->_rWhite = imagecolorallocate($this->_rImg, 255, 255, 255); imagefilledrectangle($this->_rImg, 0, 0, 399, 99, $this->_rWhite); $this->_mixing(); imageline($this->_rImg, mt_rand(2, $this->_iWidth + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(1, $this->_iHeight + $this->_iMargin), mt_rand(2, $this->_iWidth + $this->_iMargin), $this->_rBlack); imageline($this->_rImg, mt_rand(2, $this->_iHeight + $this->_iMargin), mt_rand(1, $this->_iHeight + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(2, $this->_iHeight + $this->_iMargin), $this->_rRed); imageline($this->_rImg, mt_rand(2, $this->_iHeight + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(1, $this->_iWidth + $this->_iMargin), mt_rand(2, $this->_iHeight + $this->_iMargin), $this->_aColor[array_rand($this->_aColor)]); unset($this->_rBlack, $this->_rRed, $this->_rWhite); imageconvolution($this->_rImg, $this->_aMatrixBlur, 9, 0); imageconvolution($this->_rImg, $this->_aMatrixBlur, 9, 0); unset($this->_aMatrixBlur); (new Browser())->noCache(); header('Content-type: image/png'); imagepng($this->_rImg); imagedestroy($this->_rImg); }
function compress($path, $res) { $size = getimagesize($path); $width = $size[0]; $height = $size[1]; $quality = 75; $xRatio = $res / $width; $yRatio = $res / $height; $tnHeight; $tnWidth; if ($xRatio * $height < $rse) { // Resize the image based on width $tnHeight = ceil($xRatio * $height); $tnWidth = $res; } else { $tnWidth = ceil($yRatio * $width); $tnHeight = $res; } ini_set('memory_limit', MEMORY_TO_ALLOCATE); $dst = imagecreatetruecolor($tnWidth, $tnHeight); $src; $newfile = dirname($path) . '/' . $res . '/' . $res . '_' . basename($path); switch ($size['mime']) { case 'image/gif': $src = imagecreatefromgif($path); $this->setalpha($dst); $quality = round(10 - $quality / 10); $sharpen = false; $createfile = 'imagepng'; break; case 'image/x-png': case 'image/png': $src = imagecreatefrompng($path); $this->setalpha($dst); $quality = round(10 - $quality / 10); $sharpen = false; $createfile = 'imagepng'; break; default: $src = imagecreatefromjpeg($path); $sharpen = true; $createfile = 'imagejpeg'; break; } imagecopyresampled($dst, $src, 0, 0, 0, 0, $tnWidth, $tnHeight, $width, $height); if ($sharpen) { $sharpness = $this->findSharp($width, $tnWidth); $sharpenMatrix = array(array(-1, -2, -1), array(-2, $sharpness + 12, -2), array(-1, -2, -1)); $divisor = $sharpness; $offset = 0; imageconvolution($dst, $sharpenMatrix, $divisor, $offset); } if (!file_exists(dirname($path))) { mkdir(dirname($path)); } $createfile($dst, $newfile, $quality); imagedestroy($src); imagedestroy($dst); }
/** * Executes imageconvolution() filter. * * @param WideImage_Image $image * @param array $matrix * @param numeric $div * @param numeric $offset * * @return WideImage_Image */ public function execute($image, $matrix, $div, $offset) { $new = $image->asTrueColor(); if (!imageconvolution($new->getHandle(), $matrix, $div, $offset)) { throw new WideImage_GDFunctionResultException('imageconvolution() returned false'); } return $new; }
public function transform(CGImageBase $src) { // copy the image (pixel for pixel) $_dest = new CGImageBase($src); imagecopy($_dest['rsrc'], $src['rsrc'], 0, 0, 0, 0, $src['width'], $src['height']); imageconvolution($_dest['rsrc'], $this->_matrix, $this->_divisor, 0); return $_dest; }
function execute($image, $matrix, $div, $offset) { $new = $image->asTrueColor(); if (!imageconvolution($new->getHandle(), $matrix, $div, $offset)) { JError::raiseError(500, "imageconvolution() returned false"); } return $new; }
/** * {@inheritdoc} */ public function sharpen() { $sharpenMatrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)); $divisor = array_sum(array_map('array_sum', $sharpenMatrix)); if (false === imageconvolution($this->resource, $sharpenMatrix, $divisor, 0)) { throw new RuntimeException('Failed to sharpen the image'); } return $this; }
private function applySharpening() { if (!function_exists('imageconvolution')) { return; } if ($this->sharpening !== 1) { $this->sharpening = abs(1 - $this->sharpening); } $matrix = array(array(-1, -1, -1), array(-1, ceil($this->sharpening * 60), -1), array(-1, -1, -1)); $divisor = array_sum(array_map('array_sum', $matrix)); imageconvolution($this->finalImage, $matrix, $divisor, 0); }
/** * Sharpen image * * @param Intervention\Image\Image $image * @return boolean */ public function execute($image) { $amount = $this->argument(0)->between(0, 100)->value(10); // build matrix $min = $amount >= 10 ? $amount * -0.01 : 0; $max = $amount * -0.025; $abs = (4 * $min + 4 * $max) * -1 + 1; $div = 1; $matrix = array(array($min, $max, $min), array($max, $abs, $max), array($min, $max, $min)); // apply the matrix return imageconvolution($image->getCore(), $matrix, $div, 0); }
/** * @param Image $image * * @return Image */ public function apply($image) { $amount = $this->amount; // build matrix $min = $amount >= 10 ? $amount * -0.01 : 0; $max = $amount * -0.025; $abs = (4 * $min + 4 * $max) * -1 + 1; $div = 1; $matrix = array(array($min, $max, $min), array($max, $abs, $max), array($min, $max, $min)); // apply the matrix imageconvolution($image->getCore(), $matrix, $div, 0); return $image; }
/** * imageconvolution replacement for when the gd function is missing. * * @author Chao Xu * @param himage $src The image * @param array $filter The array * @param float $div Filter divisor * @param float $offset Filter offset */ static function imageconvolution($src, $filter, $div, $offset) { if (function_exists('imageconvolution')) { return imageconvolution($himage, $m, $div, $offs); } if ($src == NULL) { return 0; } $sx = imagesx($src); $sy = imagesy($src); $srcback = ImageCreateTrueColor($sx, $sy); ImageCopy($srcback, $src, 0, 0, 0, 0, $sx, $sy); if ($srcback == NULL) { return 0; } $pxl = array(1, 1); for ($y = 0; $y < $sy; ++$y) { for ($x = 0; $x < $sx; ++$x) { $new_r = $new_g = $new_b = 0; $alpha = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_a = $alpha >> 24; for ($j = 0; $j < 3; ++$j) { $yv = min(max($y - 1 + $j, 0), $sy - 1); for ($i = 0; $i < 3; ++$i) { $pxl = array(min(max($x - 1 + $i, 0), $sx - 1), $yv); $rgb = imagecolorat($srcback, $pxl[0], $pxl[1]); $new_r += ($rgb >> 16 & 0xff) * $filter[$j][$i]; $new_g += ($rgb >> 8 & 0xff) * $filter[$j][$i]; $new_b += ($rgb & 0xff) * $filter[$j][$i]; } } $new_r = $new_r / $div + $offset; $new_g = $new_g / $div + $offset; $new_b = $new_b / $div + $offset; $new_r = $new_r > 255 ? 255 : ($new_r < 0 ? 0 : $new_r); $new_g = $new_g > 255 ? 255 : ($new_g < 0 ? 0 : $new_g); $new_b = $new_b > 255 ? 255 : ($new_b < 0 ? 0 : $new_b); $new_pxl = ImageColorAllocateAlpha($src, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); if ($new_pxl == -1) { $new_pxl = ImageColorClosestAlpha($src, (int) $new_r, (int) $new_g, (int) $new_b, $new_a); } if ($y >= 0 && $y < $sy) { imagesetpixel($src, $x, $y, $new_pxl); } } } imagedestroy($srcback); return 1; }
public function blur() { // resource $res = $this->src_image; if (is_resource($this->dst_image)) { $res = $this->dst_image; } // define matrix $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0)); // calculate the divisor $divisor = array_sum(array_map('array_sum', $gaussian)); // apply the matrix imageconvolution($res, $gaussian, $divisor, 0); return $this; }
/** * provides image blur support for lib-GD:zp_imageUnsharpMask * * @param image $imgCanvas * @param int $radius * @param int $w * @param int $h */ function imageBlurGD($imgCanvas, $imgCanvas2, $radius, $w, $h) { // Gaussian blur matrix: // 1 2 1 // 2 4 2 // 1 2 1 ////////////////////////////////////////////////// for ($i = 0; $i < $radius; $i++) { if (function_exists('imageconvolution')) { // PHP >= 5.1 $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); imageconvolution($imgCanvas, $matrix, 16, 0); } } }
/** * Method to apply a filter to an image resource. * * @param array $options An array of options for the filter. * * @return void * * @since 11.3 * @throws RuntimeException */ public function execute(array $options = array()) { // Verify that image filter support for PHP is available. if (!function_exists('imagefilter')) { // @codeCoverageIgnoreStart JLog::add('The imagefilter function for PHP is not available.', JLog::ERROR); throw new RuntimeException('The imagefilter function for PHP is not available.'); // @codeCoverageIgnoreEnd } // Sharpen the image $sharpenMatrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)); $divisor = 8; $offset = 0; imageconvolution($this->handle, $sharpenMatrix, $divisor, $offset); }
public function __construct($image, array $sharpen_matrix = null) { if (is_array($sharpen_matrix)) { $this->sharpen_matrix = $sharpen_matrix; } if ($image instanceof ImageInterface) { $image = $image->getResource(); } if (!is_resource($image)) { throw new \InvalidArgumentException("Image resource expected"); } $offset = 0; $divisor = array_sum(array_map('array_sum', $this->sharpen_matrix)); imageconvolution($image, $this->sharpen_matrix, $divisor, $offset); }
/** * Apply this filter on a given image. Note: This changes the given image! * * @param img.Image image * @return bool * @throws img.ImagingException */ public function applyOn($image) { // Use builtin function (exists as of 5.1.0) if (function_exists('imageconvolution')) { return imageconvolution($image->handle, $this->kernel->getMatrix(), $this->divisor, $this->offset); } $clone = clone $image; // Create local variables for faster access $chandle = $clone->handle; $ihandle = $image->handle; $matrix = $this->kernel->getMatrix(); $divisor = $this->divisor; $offset = $this->offset; $w = $image->getWidth(); $h = $image->getHeight(); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { $nr = $ng = $nb = 0; // Apply matrix for ($j = 0; $j < 3; $j++) { $max = $y - 1 + $j; $min = $max < 0 ? 0 : $max; $yv = $min >= $h ? $h - 1 : $min; for ($i = 0; $i < 3; $i++) { $max = $x - 1 + $i; $min = $max < 0 ? 0 : $max; $xv = $min >= $w ? $w - 1 : $min; $rgb = imagecolorat($chandle, $xv, $yv); $m = $matrix[$j][$i]; $nr += ($rgb >> 16 & 0xff) * $m; $ng += ($rgb >> 8 & 0xff) * $m; $nb += ($rgb & 0xff) * $m; } } // Apply divisor and offset $nr = $nr / $divisor + $offset; $ng = $ng / $divisor + $offset; $nb = $nb / $divisor + $offset; // Normalize $nr = $nr > 255.0 ? 255.0 : ($nr < 0.0 ? 0.0 : $nr); $ng = $ng > 255.0 ? 255.0 : ($ng < 0.0 ? 0.0 : $ng); $nb = $nb > 255.0 ? 255.0 : ($nb < 0.0 ? 0.0 : $nb); imagesetpixel($ihandle, $x, $y, imagecolorallocate($ihandle, $nr, $ng, $nb)); } } unset($clone); }
function handleImage($user) { if (isset($_FILES['image']['name'])) { $saveto = "avatars/{$user}.jpg"; move_uploaded_file($_FILES['image']['tmp_name'], $saveto); $typeok = TRUE; switch ($_FILES['image']['type']) { case "image/gif": $src = imagecreatefromgif($saveto); break; case "image/jpeg": // Both regular and progressive jpegs // Both regular and progressive jpegs case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break; case "image/png": $src = imagecreatefrompng($saveto); break; default: $typeok = FALSE; break; } if ($typeok) { list($w, $h) = getimagesize($saveto); $max = 100; $tw = $w; $th = $h; if ($w > $h && $max < $w) { $th = $max / $w * $h; $tw = $max; } elseif ($h > $w && $max < $h) { $tw = $max / $h * $w; $th = $max; } elseif ($max < $w) { $tw = $th = $max; } $tmp = imagecreatetruecolor($tw, $th); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); imageconvolution($tmp, array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0); imagejpeg($tmp, $saveto); imagedestroy($tmp); imagedestroy($src); } } }
public function run($template) { //Remove the whole string as the first result array_shift($this->URLMatch); //Get the database $dbh = Engine::getDatabase(); //Get result ID $resultID = $this->URLMatch[0]; //Check if the result is in the array and return results $sql = "SELECT * FROM Results WHERE Result_ID IN (SELECT Result_ID FROM Result_History WHERE Result_ID= :result) LIMIT 1"; $stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $stmt->execute(array(':result' => $resultID)); $result = $stmt->fetchObject('Result'); if ($result == false) { exit; } //There's no result to give an image for $result->Data = json_decode($result->Data, true); $data = $result->Data; $blueBackground = imagecreatefromstring(file_get_contents(__DIR__ . '/../public/images/share-background.png', "r")); $friends = array_keys($data['interaction']); $friend1 = imagecreatefromstring(file_get_contents(User::getAvatar($friends[0]))); //200,200 (width x height) $friend2 = imagecreatefromstring(file_get_contents(User::getAvatar($friends[1]))); $friend3 = imagecreatefromstring(file_get_contents(User::getAvatar($friends[2]))); $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0)); for ($i = 0; $i < 60; $i++) { imageconvolution($friend1, $gaussian, 16, 0); imageconvolution($friend2, $gaussian, 16, 0); imageconvolution($friend3, $gaussian, 16, 0); } $graph = imagecreatefromstring(file_get_contents(__DIR__ . '/../public/images/white-logo-transparent-medium.png', "r")); $foreground = imagecreatefromstring(file_get_contents(__DIR__ . '/../public/images/share-foreground.png', "r")); imagecopy($blueBackground, $friend1, -50, 25, 0, 0, imagesx($friend1), imagesy($friend1)); imagecopy($blueBackground, $friend2, 150, 25, 0, 0, imagesx($friend2), imagesy($friend2)); imagecopy($blueBackground, $friend3, 350, 25, 0, 0, imagesx($friend3), imagesy($friend3)); $graph = imagescale($graph, imagesx($friend1) * 2); imagecopy($blueBackground, $graph, 80, -20, 0, 0, imagesx($graph), imagesy($graph)); imagecopy($blueBackground, $foreground, 0, 0, 0, 0, imagesx($foreground), imagesy($foreground)); ob_clean(); ob_start(); header('Content-Type: image/png'); imagepng($blueBackground); }
function makeDynamicThumbnail() { global $clerk; if (isset($_GET['dynamic_thumbnail']) == false) { return; } load_helper("ThumbLib.inc"); $file = $_GET['file']; $width = $_GET['width']; $height = $_GET['height']; $adaptive = $_GET['adaptive']; $file_extension = substr($file, strrpos($file, '.')); $cache_dir = $clerk->getSetting("cache_path", 1); $cache_file_name = str_replace($file_extension, "", basename($file)) . "." . $width . "x" . $height . "_" . $adaptive . ".jpg"; $path = $cache_dir . "/" . $cache_file_name; if (!is_dir($cache_dir)) { mkdir($cache_dir); } $thumb = PhpThumbFactory::create($file, array('resizeUp' => true)); $thumb->setFormat("JPG"); if ($adaptive == 0 || ($width == 0 || $height == 0)) { $thumb->resize($width, $height); } else { $thumb->adaptiveResize($width, $height); } $thumb->save($path); $data = call_anchor("modifyDynamicThumbnail", array("path" => $path, "filename" => $cache_file_name, "orig_filename" => basename($file), "thumb" => $thumb)); // Sharpen filter // for nicer thumbnails $path = $data['path']; $i = imagecreatefromjpeg($path); $sharpen_matrix = array(array(0.0, -0.8, 0.0), array(-0.8, 6.5, -0.8), array(0.0, -0.8, 0.0)); $divisor = array_sum(array_map('array_sum', $sharpen_matrix)); imageconvolution($i, $sharpen_matrix, $divisor, 0); imagejpeg($i, $path, 100); $path = $data['path']; header('Content-type: image/jpeg'); $image = imagecreatefromjpeg($path); imagejpeg($image, null, 100); imagedestroy($image); exit; }
public function apply($resource) { // Extract arguments @(list(, $matrix, $offset) = func_get_args()); $matrix = (array) $matrix; $offset = (int) $offset; // Verify matrix if (count($matrix) != 9) { throw new Exception("The array parameter is a 3x3 matrix and must so contain 9 rows"); } // Normalization $matrix = array(array($matrix[0], $matrix[1], $matrix[2]), array($matrix[3], $matrix[4], $matrix[5]), array($matrix[6], $matrix[7], $matrix[8])); // Compute normalization divisor $div = array_sum($matrix[0]) + array_sum($matrix[1]) + array_sum($matrix[2]); // Apply effect if (!imageconvolution($resource, $matrix, $div, $offset)) { throw new Exception("Image convolution failed"); } return $resource; }
public function __construct($dbo = NULL, $imgFilename, $photoSize = 160) { parent::__construct($dbo); $this->imgFilename = $imgFilename; $this->photoSize = $photoSize; $this->initialfilesize = filesize($imgFilename); $this->imageproperties = getimagesize($imgFilename); $this->mimetype = image_type_to_mime_type($this->imageproperties[2]); if ($this->imageproperties[2] === IMAGETYPE_JPEG) { $this->imgData = imagecreatefromjpeg($this->imgFilename); } else { $this->imgData = imagecreatefrompng($this->imgFilename); } $srcW = $this->imageproperties[0]; $srcH = $this->imageproperties[1]; if ($srcW > $this->photoSize || $srcH > $this->photoSize) { if ($srcW < $srcH) { $koe = $srcW / $this->photoSize; $destW = $this->photoSize; $destH = ceil($srcH / $koe); $src_x = $destW / 2 - $this->photoSize / 2; $src_y = 0; } else { $koe = $srcH / $this->photoSize; $destH = $this->photoSize; $destW = ceil($srcW / $koe); $src_x = $destW / 2 - $this->photoSize / 2; $src_y = 0; } $copy = imagecreatetruecolor($destW, $destH); imagecopyresampled($copy, $this->imgData, 0, 0, 0, 0, $destW, $destH, imagesx($this->imgData), imagesy($this->imgData)); imagedestroy($this->imgData); imageconvolution($copy, array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0); $this->imgData = imagecreatetruecolor($this->photoSize, $this->photoSize); imagecopy($this->imgData, $copy, 0, 0, $src_x, $src_y, $this->photoSize, $this->photoSize); imagedestroy($copy); } }
/** * Actually uploads the file, and act on it according to the set processing class variables * * This function copies the uploaded file to the given location, eventually performing actions on it. * Typically, you can call {@link process} several times for the same file, * for instance to create a resized image and a thumbnail of the same file. * The original uploaded file remains intact in its temporary location, so you can use {@link process} several times. * You will be able to delete the uploaded file with {@link clean} when you have finished all your {@link process} calls. * * According to the processing class variables set in the calling file, the file can be renamed, * and if it is an image, can be resized or converted. * * When the processing is completed, and the file copied to its new location, the * processing class variables will be reset to their default value. * This allows you to set new properties, and perform another {@link process} on the same uploaded file * * If the function is called with a null or empty argument, then it will return the content of the picture * * It will set {@link processed} (and {@link error} is an error occurred) * * @access public * @param string $server_path Optional path location of the uploaded file, with an ending slash * @return string Optional content of the image */ function process($server_path = null) { $this->error = ''; $this->processed = true; $return_mode = false; $return_content = null; // clean up dst variables $this->file_dst_path = ''; $this->file_dst_pathname = ''; $this->file_dst_name = ''; $this->file_dst_name_body = ''; $this->file_dst_name_ext = ''; if (!$this->uploaded) { $this->error = $this->translate('file_not_uploaded'); $this->processed = false; } if ($this->processed) { if (empty($server_path) || is_null($server_path)) { $this->log .= '<b>process file and return the content</b><br />'; $return_mode = true; } else { if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { if (substr($server_path, -1, 1) != '\\') { $server_path = $server_path . '\\'; } } else { if (substr($server_path, -1, 1) != '/') { $server_path = $server_path . '/'; } } $this->log .= '<b>process file to ' . $server_path . '</b><br />'; } } if ($this->processed) { // checks file max size if ($this->file_src_size > $this->file_max_size) { $this->processed = false; $this->error = $this->translate('file_too_big'); } else { $this->log .= '- file size OK<br />'; } } if ($this->processed) { // if we have an image without extension, set it if ($this->file_is_image && !$this->file_src_name_ext_) { $this->file_src_name_ext = $this->file_src_name_ext_ = $this->image_src_type; } // turn dangerous scripts into text files if ($this->no_script) { if ((substr($this->file_src_mime, 0, 5) == 'text/' && $this->file_src_mime != 'text/rtf' || strpos($this->file_src_mime, 'javascript') !== false) && substr($this->file_src_name, -4) != '.txt' || preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $this->file_src_name) || empty($this->file_src_name_ext_)) { $this->file_src_mime = 'text/plain'; $this->log .= '- script ' . $this->file_src_name . ' renamed as ' . $this->file_src_name . '.txt!<br />'; $this->file_src_name_ext = $this->file_src_name_ext_ . (empty($this->file_src_name_ext_) ? 'txt' : '.txt'); } } if ($this->mime_check && empty($this->file_src_mime)) { $this->processed = false; $this->error = $this->translate('no_mime'); } else { if ($this->mime_check && !empty($this->file_src_mime) && strpos($this->file_src_mime, '/') !== false) { list($m1, $m2) = explode('/', $this->file_src_mime); $allowed = false; // check wether the mime type is allowed foreach ($this->allowed as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = true; break; } } // check wether the mime type is forbidden foreach ($this->forbidden as $k => $v) { list($v1, $v2) = explode('/', $v); if ($v1 == '*' && $v2 == '*' || $v1 == $m1 && ($v2 == $m2 || $v2 == '*')) { $allowed = false; break; } } if (!$allowed) { $this->processed = false; $this->error = $this->translate('incorrect_file'); } else { $this->log .= '- file mime OK : ' . $this->file_src_mime . '<br />'; } } else { $this->log .= '- file mime (not checked) : ' . $this->file_src_mime . '<br />'; } } // if the file is an image, we can check on its dimensions // these checks are not available if open_basedir restrictions are in place if ($this->file_is_image) { if (is_numeric($this->image_src_x) && is_numeric($this->image_src_y)) { $ratio = $this->image_src_x / $this->image_src_y; if (!is_null($this->image_max_width) && $this->image_src_x > $this->image_max_width) { $this->processed = false; $this->error = $this->translate('image_too_wide'); } if (!is_null($this->image_min_width) && $this->image_src_x < $this->image_min_width) { $this->processed = false; $this->error = $this->translate('image_too_narrow'); } if (!is_null($this->image_max_height) && $this->image_src_y > $this->image_max_height) { $this->processed = false; $this->error = $this->translate('image_too_high'); } if (!is_null($this->image_min_height) && $this->image_src_y < $this->image_min_height) { $this->processed = false; $this->error = $this->translate('image_too_short'); } if (!is_null($this->image_max_ratio) && $ratio > $this->image_max_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_high'); } if (!is_null($this->image_min_ratio) && $ratio < $this->image_min_ratio) { $this->processed = false; $this->error = $this->translate('ratio_too_low'); } if (!is_null($this->image_max_pixels) && $this->image_src_pixels > $this->image_max_pixels) { $this->processed = false; $this->error = $this->translate('too_many_pixels'); } if (!is_null($this->image_min_pixels) && $this->image_src_pixels < $this->image_min_pixels) { $this->processed = false; $this->error = $this->translate('not_enough_pixels'); } } else { $this->log .= '- no image properties available, can\'t enforce dimension checks : ' . $this->file_src_mime . '<br />'; } } } if ($this->processed) { $this->file_dst_path = $server_path; // repopulate dst variables from src $this->file_dst_name = $this->file_src_name; $this->file_dst_name_body = $this->file_src_name_body; $this->file_dst_name_ext = $this->file_src_name_ext; if ($this->file_overwrite) { $this->file_auto_rename = false; } if ($this->image_convert != '') { // if we convert as an image $this->file_dst_name_ext = $this->image_convert; $this->log .= '- new file name ext : ' . $this->image_convert . '<br />'; } if ($this->file_new_name_body != '') { // rename file body $this->file_dst_name_body = $this->file_new_name_body; $this->log .= '- new file name body : ' . $this->file_new_name_body . '<br />'; } if ($this->file_new_name_ext != '') { // rename file ext $this->file_dst_name_ext = $this->file_new_name_ext; $this->log .= '- new file name ext : ' . $this->file_new_name_ext . '<br />'; } if ($this->file_name_body_add != '') { // append a string to the name $this->file_dst_name_body = $this->file_dst_name_body . $this->file_name_body_add; $this->log .= '- file name body append : ' . $this->file_name_body_add . '<br />'; } if ($this->file_name_body_pre != '') { // prepend a string to the name $this->file_dst_name_body = $this->file_name_body_pre . $this->file_dst_name_body; $this->log .= '- file name body prepend : ' . $this->file_name_body_pre . '<br />'; } if ($this->file_safe_name) { // formats the name $this->file_dst_name_body = str_replace(array(' ', '-'), array('_', '_'), $this->file_dst_name_body); $this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body); $this->log .= '- file name safe format<br />'; } $this->log .= '- destination variables<br />'; if (empty($this->file_dst_path) || is_null($this->file_dst_path)) { $this->log .= ' file_dst_path : n/a<br />'; } else { $this->log .= ' file_dst_path : ' . $this->file_dst_path . '<br />'; } $this->log .= ' file_dst_name_body : ' . $this->file_dst_name_body . '<br />'; $this->log .= ' file_dst_name_ext : ' . $this->file_dst_name_ext . '<br />'; // do we do some image manipulation? $image_manipulation = $this->file_is_image && ($this->image_resize || $this->image_convert != '' || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || is_numeric($this->image_threshold) || !empty($this->image_tint_color) || !empty($this->image_overlay_color) || $this->image_unsharp || !empty($this->image_text) || $this->image_greyscale || $this->image_negative || !empty($this->image_watermark) || is_numeric($this->image_rotate) || is_numeric($this->jpeg_size) || !empty($this->image_flip) || !empty($this->image_crop) || !empty($this->image_precrop) || !empty($this->image_border) || $this->image_frame > 0 || $this->image_bevel > 0 || $this->image_reflection_height); if ($image_manipulation) { if ($this->image_convert == '') { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- image operation, keep extension<br />'; } else { $this->file_dst_name = $this->file_dst_name_body . '.' . $this->image_convert; $this->log .= '- image operation, change extension for conversion type<br />'; } } else { $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $this->log .= '- no image operation, keep extension<br />'; } if (!$return_mode) { if (!$this->file_auto_rename) { $this->log .= '- no auto_rename if same filename exists<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } else { $this->log .= '- checking for auto_rename<br />'; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; $body = $this->file_dst_name_body; $cpt = 1; while (@file_exists($this->file_dst_pathname)) { $this->file_dst_name_body = $body . '_' . $cpt; $this->file_dst_name = $this->file_dst_name_body . (!empty($this->file_dst_name_ext) ? '.' . $this->file_dst_name_ext : ''); $cpt++; $this->file_dst_pathname = $this->file_dst_path . $this->file_dst_name; } if ($cpt > 1) { $this->log .= ' auto_rename to ' . $this->file_dst_name . '<br />'; } } $this->log .= '- destination file details<br />'; $this->log .= ' file_dst_name : ' . $this->file_dst_name . '<br />'; $this->log .= ' file_dst_pathname : ' . $this->file_dst_pathname . '<br />'; if ($this->file_overwrite) { $this->log .= '- no overwrite checking<br />'; } else { if (@file_exists($this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('already_exists', array($this->file_dst_name)); } else { $this->log .= '- ' . $this->file_dst_name . ' doesn\'t exist already<br />'; } } } } if ($this->processed) { // if we have already moved the uploaded file, we use the temporary copy as source file, and check if it exists if (!empty($this->file_src_temp)) { $this->log .= '- use the temp file instead of the original file since it is a second process<br />'; $this->file_src_pathname = $this->file_src_temp; if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('temp_file_missing'); } // if we haven't a temp file, and that we do check on uploads, we use is_uploaded_file() } else { if (!$this->no_upload_check) { if (!is_uploaded_file($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } // otherwise, if we don't check on uploaded files (local file for instance), we use file_exists() } else { if (!file_exists($this->file_src_pathname)) { $this->processed = false; $this->error = $this->translate('source_missing'); } } } // checks if the destination directory exists, and attempt to create it if (!$return_mode) { if ($this->processed && !file_exists($this->file_dst_path)) { if ($this->dir_auto_create) { $this->log .= '- ' . $this->file_dst_path . ' doesn\'t exist. Attempting creation:'; if (!$this->rmkdir($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir'); } else { $this->log .= ' success<br />'; } } else { $this->error = $this->translate('destination_dir_missing'); } } if ($this->processed && !is_dir($this->file_dst_path)) { $this->processed = false; $this->error = $this->translate('destination_path_not_dir'); } // checks if the destination directory is writeable, and attempt to make it writeable $hash = md5($this->file_dst_name_body . rand(1, 1000)); if ($this->processed && !($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { if ($this->dir_auto_chmod) { $this->log .= '- ' . $this->file_dst_path . ' is not writeable. Attempting chmod:'; if (!@chmod($this->file_dst_path, $this->dir_chmod)) { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { $this->log .= ' success<br />'; if (!($f = @fopen($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext, 'a+'))) { // we re-check $this->processed = false; $this->error = $this->translate('destination_dir_write'); } else { @fclose($f); } } } else { $this->processed = false; $this->error = $this->translate('destination_path_write'); } } else { if ($this->processed) { @fclose($f); } @unlink($this->file_dst_path . $hash . '.' . $this->file_dst_name_ext); } // if we have an uploaded file, and if it is the first process, and if we can't access the file directly (open_basedir restriction) // then we create a temp file that will be used as the source file in subsequent processes // the third condition is there to check if the file is not accessible *directly* (it already has positively gone through is_uploaded_file(), so it exists) if (!$this->no_upload_check && empty($this->file_src_temp) && !@file_exists($this->file_src_pathname)) { $this->log .= '- attempting to use a temp file:'; $hash = md5($this->file_dst_name_body . rand(1, 1000)); if (move_uploaded_file($this->file_src_pathname, $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext)) { $this->file_src_pathname = $this->file_dst_path . $hash . '.' . $this->file_dst_name_ext; $this->file_src_temp = $this->file_src_pathname; $this->log .= ' file created<br />'; $this->log .= ' temp file is: ' . $this->file_src_temp . '<br />'; } else { $this->log .= ' failed<br />'; $this->processed = false; $this->error = $this->translate('temp_file'); } } } } if ($this->processed) { // we do a quick check to ensure the file is really an image // we can do this only now, as it would have failed before in case of open_basedir if ($image_manipulation && !@getimagesize($this->file_src_pathname)) { $this->log .= '- the file is not an image!<br />'; $image_manipulation = false; } if ($image_manipulation) { // checks if the source file is readable if ($this->processed && !($f = @fopen($this->file_src_pathname, 'r'))) { $this->processed = false; $this->error = $this->translate('source_not_readable'); } else { @fclose($f); } // we now do all the image manipulations $this->log .= '- image resizing or conversion wanted<br />'; if ($this->gdversion()) { switch ($this->image_src_type) { case 'jpg': if (!function_exists('imagecreatefromjpeg')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('JPEG')); } else { $image_src = @imagecreatefromjpeg($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('JPEG')); } else { $this->log .= '- source image is JPEG<br />'; } } break; case 'png': if (!function_exists('imagecreatefrompng')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('PNG')); } else { $image_src = @imagecreatefrompng($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('PNG')); } else { $this->log .= '- source image is PNG<br />'; } } break; case 'gif': if (!function_exists('imagecreatefromgif')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('GIF')); } else { $image_src = @imagecreatefromgif($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('GIF')); } else { $this->log .= '- source image is GIF<br />'; } } break; case 'bmp': if (!method_exists($this, 'imagecreatefrombmp')) { $this->processed = false; $this->error = $this->translate('no_create_support', array('BMP')); } else { $image_src = @$this->imagecreatefrombmp($this->file_src_pathname); if (!$image_src) { $this->processed = false; $this->error = $this->translate('create_error', array('BMP')); } else { $this->log .= '- source image is BMP<br />'; } } break; default: $this->processed = false; $this->error = $this->translate('source_invalid'); } } else { $this->processed = false; $this->error = $this->translate('gd_missing'); } if ($this->processed && $image_src) { // we have to set image_convert if it is not already if (empty($this->image_convert)) { $this->log .= '- setting destination file type to ' . $this->file_src_name_ext . '<br />'; $this->image_convert = $this->file_src_name_ext; } if (!in_array($this->image_convert, $this->image_supported)) { $this->image_convert = 'jpg'; } // we set the default color to be the background color if we don't output in a transparent format if ($this->image_convert != 'png' && $this->image_convert != 'gif' && !empty($this->image_default_color) && empty($this->image_background_color)) { $this->image_background_color = $this->image_default_color; } if (!empty($this->image_background_color)) { $this->image_default_color = $this->image_background_color; } if (empty($this->image_default_color)) { $this->image_default_color = '#FFFFFF'; } $this->image_src_x = imagesx($image_src); $this->image_src_y = imagesy($image_src); $gd_version = $this->gdversion(); $ratio_crop = null; if (!imageistruecolor($image_src)) { // $this->image_src_type == 'gif' $this->log .= '- image is detected as having a palette<br />'; $this->image_is_palette = true; $this->image_transparent_color = imagecolortransparent($image_src); if ($this->image_transparent_color >= 0 && imagecolorstotal($image_src) > $this->image_transparent_color) { $this->image_is_transparent = true; $this->log .= ' palette image is detected as transparent<br />'; } // if the image has a palette (GIF), we convert it to true color, preserving transparency $this->log .= ' convert palette image to true color<br />'; $true_color = imagecreatetruecolor($this->image_src_x, $this->image_src_y); imagealphablending($true_color, false); imagesavealpha($true_color, true); for ($x = 0; $x < $this->image_src_x; $x++) { for ($y = 0; $y < $this->image_src_y; $y++) { if ($this->image_transparent_color >= 0 && imagecolorat($image_src, $x, $y) == $this->image_transparent_color) { imagesetpixel($true_color, $x, $y, 127 << 24); } else { $rgb = imagecolorsforindex($image_src, imagecolorat($image_src, $x, $y)); imagesetpixel($true_color, $x, $y, $rgb['alpha'] << 24 | $rgb['red'] << 16 | $rgb['green'] << 8 | $rgb['blue']); } } } $image_src = $this->imagetransfer($true_color, $image_src); imagealphablending($image_src, false); imagesavealpha($image_src, true); $this->image_is_palette = false; } $image_dst =& $image_src; // pre-crop image, before resizing if (!empty($this->image_precrop)) { if (is_array($this->image_precrop)) { $vars = $this->image_precrop; } else { $vars = explode(' ', $this->image_precrop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_src_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_src_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_src_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_src_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->log .= '- pre-crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_src_x = $this->image_src_x - $cl - $cr; $this->image_src_y = $this->image_src_y - $ct - $cb; if ($this->image_src_x < 1) { $this->image_src_x = 1; } if ($this->image_src_y < 1) { $this->image_src_y = 1; } $tmp = $this->imagecreatenew($this->image_src_x, $this->image_src_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_src_x, $this->image_src_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_src_x, -$ct, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_src_x + $cr, 0, $this->image_src_x, $this->image_src_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_src_y + $cb, $this->image_src_x, $this->image_src_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl, $this->image_src_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // resize image (and move image_src_x, image_src_y dimensions into image_dst_x, image_dst_y) if ($this->image_resize) { $this->log .= '- resizing...<br />'; if ($this->image_ratio_x) { $this->log .= ' calculate x size<br />'; $this->image_dst_x = round($this->image_src_x * $this->image_y / $this->image_src_y); $this->image_dst_y = $this->image_y; } else { if ($this->image_ratio_y) { $this->log .= ' calculate y size<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = round($this->image_src_y * $this->image_x / $this->image_src_x); } else { if (is_numeric($this->image_ratio_pixels)) { $this->log .= ' calculate x/y size to match a number of pixels<br />'; $pixels = $this->image_src_y * $this->image_src_x; $diff = sqrt($this->image_ratio_pixels / $pixels); $this->image_dst_x = round($this->image_src_x * $diff); $this->image_dst_y = round($this->image_src_y * $diff); } else { if ($this->image_ratio || $this->image_ratio_crop || $this->image_ratio_fill || $this->image_ratio_no_zoom_in || $this->image_ratio_no_zoom_out) { $this->log .= ' check x/y sizes<br />'; if (!$this->image_ratio_no_zoom_in && !$this->image_ratio_no_zoom_out || $this->image_ratio_no_zoom_in && ($this->image_src_x > $this->image_x || $this->image_src_y > $this->image_y) || $this->image_ratio_no_zoom_out && $this->image_src_x < $this->image_x && $this->image_src_y < $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; if ($this->image_ratio_crop) { if (!is_string($this->image_ratio_crop)) { $this->image_ratio_crop = ''; } $this->image_ratio_crop = strtolower($this->image_ratio_crop); if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_crop, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_crop, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_crop_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_crop, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_crop, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_crop_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_ratio_fill) { if (!is_string($this->image_ratio_fill)) { $this->image_ratio_fill = ''; } $this->image_ratio_fill = strtolower($this->image_ratio_fill); if ($this->image_src_x / $this->image_x < $this->image_src_y / $this->image_y) { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); $ratio_crop = array(); $ratio_crop['x'] = $this->image_dst_x - $this->image_x; if (strpos($this->image_ratio_fill, 'l') !== false) { $ratio_crop['l'] = 0; $ratio_crop['r'] = $ratio_crop['x']; } else { if (strpos($this->image_ratio_fill, 'r') !== false) { $ratio_crop['l'] = $ratio_crop['x']; $ratio_crop['r'] = 0; } else { $ratio_crop['l'] = round($ratio_crop['x'] / 2); $ratio_crop['r'] = $ratio_crop['x'] - $ratio_crop['l']; } } $this->log .= ' ratio_fill_x : ' . $ratio_crop['x'] . ' (' . $ratio_crop['l'] . ';' . $ratio_crop['r'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } else { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); $ratio_crop = array(); $ratio_crop['y'] = $this->image_dst_y - $this->image_y; if (strpos($this->image_ratio_fill, 't') !== false) { $ratio_crop['t'] = 0; $ratio_crop['b'] = $ratio_crop['y']; } else { if (strpos($this->image_ratio_fill, 'b') !== false) { $ratio_crop['t'] = $ratio_crop['y']; $ratio_crop['b'] = 0; } else { $ratio_crop['t'] = round($ratio_crop['y'] / 2); $ratio_crop['b'] = $ratio_crop['y'] - $ratio_crop['t']; } } $this->log .= ' ratio_fill_y : ' . $ratio_crop['y'] . ' (' . $ratio_crop['t'] . ';' . $ratio_crop['b'] . ')<br />'; if (is_null($this->image_crop)) { $this->image_crop = array(0, 0, 0, 0); } } } else { if ($this->image_src_x / $this->image_x > $this->image_src_y / $this->image_y) { $this->image_dst_x = $this->image_x; $this->image_dst_y = intval($this->image_src_y * ($this->image_x / $this->image_src_x)); } else { $this->image_dst_y = $this->image_y; $this->image_dst_x = intval($this->image_src_x * ($this->image_y / $this->image_src_y)); } } } } else { $this->log .= ' doesn\'t calculate x/y sizes<br />'; $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } } else { $this->log .= ' use plain sizes<br />'; $this->image_dst_x = $this->image_x; $this->image_dst_y = $this->image_y; } } } } if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); if ($gd_version >= 2) { $res = imagecopyresampled($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } else { $res = imagecopyresized($tmp, $image_src, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_src_x, $this->image_src_y); } $this->log .= ' resized image object created<br />'; $this->log .= ' image_src_x y : ' . $this->image_src_x . ' x ' . $this->image_src_y . '<br />'; $this->log .= ' image_dst_x y : ' . $this->image_dst_x . ' x ' . $this->image_dst_y . '<br />'; // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } else { $this->image_dst_x = $this->image_src_x; $this->image_dst_y = $this->image_src_y; } // crop image (and also crops if image_ratio_crop is used) if (!empty($this->image_crop) || !is_null($ratio_crop)) { if (is_array($this->image_crop)) { $vars = $this->image_crop; } else { $vars = explode(' ', $this->image_crop); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; // we adjust the cropping if we use image_ratio_crop if (!is_null($ratio_crop)) { if (array_key_exists('t', $ratio_crop)) { $ct += $ratio_crop['t']; } if (array_key_exists('r', $ratio_crop)) { $cr += $ratio_crop['r']; } if (array_key_exists('b', $ratio_crop)) { $cb += $ratio_crop['b']; } if (array_key_exists('l', $ratio_crop)) { $cl += $ratio_crop['l']; } } $this->log .= '- crop image : ' . $ct . ' ' . $cr . ' ' . $cb . ' ' . $cl . ' <br />'; $this->image_dst_x = $this->image_dst_x - $cl - $cr; $this->image_dst_y = $this->image_dst_y - $ct - $cb; if ($this->image_dst_x < 1) { $this->image_dst_x = 1; } if ($this->image_dst_y < 1) { $this->image_dst_y = 1; } $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); // we copy the image into the recieving image imagecopy($tmp, $image_dst, 0, 0, $cl, $ct, $this->image_dst_x, $this->image_dst_y); // if we crop with negative margins, we have to make sure the extra bits are the right color, or transparent if ($ct < 0 || $cr < 0 || $cb < 0 || $cl < 0) { // use the background color if present if (!empty($this->image_background_color)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $fill = imagecolorallocate($tmp, $red, $green, $blue); } else { $fill = imagecolorallocatealpha($tmp, 0, 0, 0, 127); } // fills eventual negative margins if ($ct < 0) { imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, -$ct - 1, $fill); } if ($cr < 0) { imagefilledrectangle($tmp, $this->image_dst_x + $cr, 0, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cb < 0) { imagefilledrectangle($tmp, 0, $this->image_dst_y + $cb, $this->image_dst_x, $this->image_dst_y, $fill); } if ($cl < 0) { imagefilledrectangle($tmp, 0, 0, -$cl - 1, $this->image_dst_y, $fill); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // flip image if ($gd_version >= 2 && !empty($this->image_flip)) { $this->image_flip = strtolower($this->image_flip); $this->log .= '- flip image : ' . $this->image_flip . '<br />'; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if (strpos($this->image_flip, 'v') !== false) { imagecopy($tmp, $image_dst, $this->image_dst_x - $x - 1, $y, $x, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $this->image_dst_y - $y - 1, $x, $y, 1, 1); } } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // rotate image if ($gd_version >= 2 && is_numeric($this->image_rotate)) { if (!in_array($this->image_rotate, array(0, 90, 180, 270))) { $this->image_rotate = 0; } if ($this->image_rotate != 0) { if ($this->image_rotate == 90 || $this->image_rotate == 270) { $tmp = $this->imagecreatenew($this->image_dst_y, $this->image_dst_x); } else { $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); } $this->log .= '- rotate image : ' . $this->image_rotate . '<br />'; for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { if ($this->image_rotate == 90) { imagecopy($tmp, $image_dst, $y, $x, $x, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 180) { imagecopy($tmp, $image_dst, $x, $y, $this->image_dst_x - $x - 1, $this->image_dst_y - $y - 1, 1, 1); } else { if ($this->image_rotate == 270) { imagecopy($tmp, $image_dst, $y, $x, $this->image_dst_x - $x - 1, $y, 1, 1); } else { imagecopy($tmp, $image_dst, $x, $y, $x, $y, 1, 1); } } } } } if ($this->image_rotate == 90 || $this->image_rotate == 270) { $t = $this->image_dst_y; $this->image_dst_y = $this->image_dst_x; $this->image_dst_x = $t; } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } } // unsharp mask if ($gd_version >= 2 && $this->image_unsharp && is_numeric($this->image_unsharp_amount) && is_numeric($this->image_unsharp_radius) && is_numeric($this->image_unsharp_threshold)) { // Unsharp Mask for PHP - version 2.1.1 // Unsharp mask algorithm by Torstein Hønsi 2003-07. // Used with permission // Modified to support alpha transparency if ($this->image_unsharp_amount > 500) { $this->image_unsharp_amount = 500; } $this->image_unsharp_amount = $this->image_unsharp_amount * 0.016; if ($this->image_unsharp_radius > 50) { $this->image_unsharp_radius = 50; } $this->image_unsharp_radius = $this->image_unsharp_radius * 2; if ($this->image_unsharp_threshold > 255) { $this->image_unsharp_threshold = 255; } $this->image_unsharp_radius = abs(round($this->image_unsharp_radius)); if ($this->image_unsharp_radius != 0) { $this->image_dst_x = imagesx($image_dst); $this->image_dst_y = imagesy($image_dst); $canvas = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); $blur = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y, false, true); if (function_exists('imageconvolution')) { // PHP >= 5.1 $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); imagecopy($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imageconvolution($blur, $matrix, 16, 0); } else { for ($i = 0; $i < $this->image_unsharp_radius; $i++) { imagecopy($blur, $image_dst, 0, 0, 1, 0, $this->image_dst_x - 1, $this->image_dst_y); // left $this->imagecopymergealpha($blur, $image_dst, 1, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // right $this->imagecopymergealpha($blur, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, 50); // center imagecopy($canvas, $blur, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); $this->imagecopymergealpha($blur, $canvas, 0, 0, 0, 1, $this->image_dst_x, $this->image_dst_y - 1, 33.33333); // up $this->imagecopymergealpha($blur, $canvas, 0, 1, 0, 0, $this->image_dst_x, $this->image_dst_y, 25); // down } } $p_new = array(); if ($this->image_unsharp_threshold > 0) { for ($x = 0; $x < $this->image_dst_x - 1; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = abs($p_orig['red'] - $p_blur['red']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red'])) : $p_orig['red']; $p_new['green'] = abs($p_orig['green'] - $p_blur['green']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green'])) : $p_orig['green']; $p_new['blue'] = abs($p_orig['blue'] - $p_blur['blue']) >= $this->image_unsharp_threshold ? max(0, min(255, $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue'])) : $p_orig['blue']; if ($p_orig['red'] != $p_new['red'] || $p_orig['green'] != $p_new['green'] || $p_orig['blue'] != $p_new['blue']) { $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } else { for ($x = 0; $x < $this->image_dst_x; $x++) { for ($y = 0; $y < $this->image_dst_y; $y++) { $p_orig = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $p_blur = imagecolorsforindex($blur, imagecolorat($blur, $x, $y)); $p_new['red'] = $this->image_unsharp_amount * ($p_orig['red'] - $p_blur['red']) + $p_orig['red']; if ($p_new['red'] > 255) { $p_new['red'] = 255; } elseif ($p_new['red'] < 0) { $p_new['red'] = 0; } $p_new['green'] = $this->image_unsharp_amount * ($p_orig['green'] - $p_blur['green']) + $p_orig['green']; if ($p_new['green'] > 255) { $p_new['green'] = 255; } elseif ($p_new['green'] < 0) { $p_new['green'] = 0; } $p_new['blue'] = $this->image_unsharp_amount * ($p_orig['blue'] - $p_blur['blue']) + $p_orig['blue']; if ($p_new['blue'] > 255) { $p_new['blue'] = 255; } elseif ($p_new['blue'] < 0) { $p_new['blue'] = 0; } $color = imagecolorallocatealpha($image_dst, $p_new['red'], $p_new['green'], $p_new['blue'], $p_orig['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } imagedestroy($canvas); imagedestroy($blur); } } // add color overlay if ($gd_version >= 2 && (is_numeric($this->image_overlay_percent) && $this->image_overlay_percent > 0 && !empty($this->image_overlay_color))) { $this->log .= '- apply color overlay<br />'; list($red, $green, $blue) = $this->getcolors($this->image_overlay_color); $filter = imagecreatetruecolor($this->image_dst_x, $this->image_dst_y); $color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $this->image_dst_x, $this->image_dst_y, $color); $this->imagecopymergealpha($image_dst, $filter, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y, $this->image_overlay_percent); imagedestroy($filter); } // add brightness, contrast and tint, turns to greyscale and inverts colors if ($gd_version >= 2 && ($this->image_negative || $this->image_greyscale || is_numeric($this->image_threshold) || is_numeric($this->image_brightness) || is_numeric($this->image_contrast) || !empty($this->image_tint_color))) { $this->log .= '- apply tint, light, contrast correction, negative, greyscale and threshold<br />'; if (!empty($this->image_tint_color)) { list($tint_red, $tint_green, $tint_blue) = $this->getcolors($this->image_tint_color); } imagealphablending($image_dst, true); for ($y = 0; $y < $this->image_dst_y; $y++) { for ($x = 0; $x < $this->image_dst_x; $x++) { if ($this->image_greyscale) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = $g = $b = round(0.2125 * $pixel['red'] + 0.7154 * $pixel['green'] + 0.0721 * $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_threshold)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $c = round($pixel['red'] + $pixel['green'] + $pixel['blue']) / 3 - 127; $r = $g = $b = $c > $this->image_threshold ? 255 : 0; $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_brightness)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round($pixel['red'] + $this->image_brightness * 2), 255), 0); $g = max(min(round($pixel['green'] + $this->image_brightness * 2), 255), 0); $b = max(min(round($pixel['blue'] + $this->image_brightness * 2), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (is_numeric($this->image_contrast)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = max(min(round(($this->image_contrast + 128) * $pixel['red'] / 128), 255), 0); $g = max(min(round(($this->image_contrast + 128) * $pixel['green'] / 128), 255), 0); $b = max(min(round(($this->image_contrast + 128) * $pixel['blue'] / 128), 255), 0); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_tint_color)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = min(round($tint_red * $pixel['red'] / 169), 255); $g = min(round($tint_green * $pixel['green'] / 169), 255); $b = min(round($tint_blue * $pixel['blue'] / 169), 255); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } if (!empty($this->image_negative)) { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); $r = round(255 - $pixel['red']); $g = round(255 - $pixel['green']); $b = round(255 - $pixel['blue']); $color = imagecolorallocatealpha($image_dst, $r, $g, $b, $pixel['alpha']); imagesetpixel($image_dst, $x, $y, $color); } } } } // adds a border if ($gd_version >= 2 && !empty($this->image_border)) { if (is_array($this->image_border)) { $vars = $this->image_border; $this->log .= '- add border : ' . implode(' ', $this->image_border) . '<br />'; } else { $this->log .= '- add border : ' . $this->image_border . '<br />'; $vars = explode(' ', $this->image_border); } if (sizeof($vars) == 4) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[2]; $cl = $vars[3]; } else { if (sizeof($vars) == 2) { $ct = $vars[0]; $cr = $vars[1]; $cb = $vars[0]; $cl = $vars[1]; } else { $ct = $vars[0]; $cr = $vars[0]; $cb = $vars[0]; $cl = $vars[0]; } } if (strpos($ct, '%') > 0) { $ct = $this->image_dst_y * (str_replace('%', '', $ct) / 100); } if (strpos($cr, '%') > 0) { $cr = $this->image_dst_x * (str_replace('%', '', $cr) / 100); } if (strpos($cb, '%') > 0) { $cb = $this->image_dst_y * (str_replace('%', '', $cb) / 100); } if (strpos($cl, '%') > 0) { $cl = $this->image_dst_x * (str_replace('%', '', $cl) / 100); } if (strpos($ct, 'px') > 0) { $ct = str_replace('px', '', $ct); } if (strpos($cr, 'px') > 0) { $cr = str_replace('px', '', $cr); } if (strpos($cb, 'px') > 0) { $cb = str_replace('px', '', $cb); } if (strpos($cl, 'px') > 0) { $cl = str_replace('px', '', $cl); } $ct = (int) $ct; $cr = (int) $cr; $cb = (int) $cb; $cl = (int) $cl; $this->image_dst_x = $this->image_dst_x + $cl + $cr; $this->image_dst_y = $this->image_dst_y + $ct + $cb; if (!empty($this->image_border_color)) { list($red, $green, $blue) = $this->getcolors($this->image_border_color); } // we now create an image, that we fill with the border color $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); $background = imagecolorallocatealpha($tmp, $red, $green, $blue, 0); imagefilledrectangle($tmp, 0, 0, $this->image_dst_x, $this->image_dst_y, $background); // we then copy the source image into the new image, without merging so that only the border is actually kept imagecopy($tmp, $image_dst, $cl, $ct, 0, 0, $this->image_dst_x - $cr - $cl, $this->image_dst_y - $cb - $ct); // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add frame border if (is_numeric($this->image_frame)) { if (is_array($this->image_frame_colors)) { $vars = $this->image_frame_colors; $this->log .= '- add frame : ' . implode(' ', $this->image_frame_colors) . '<br />'; } else { $this->log .= '- add frame : ' . $this->image_frame_colors . '<br />'; $vars = explode(' ', $this->image_frame_colors); } $nb = sizeof($vars); $this->image_dst_x = $this->image_dst_x + $nb * 2; $this->image_dst_y = $this->image_dst_y + $nb * 2; $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, $nb, $nb, 0, 0, $this->image_dst_x - $nb * 2, $this->image_dst_y - $nb * 2); for ($i = 0; $i < $nb; $i++) { list($red, $green, $blue) = $this->getcolors($vars[$i]); $c = imagecolorallocate($tmp, $red, $green, $blue); if ($this->image_frame == 1) { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } else { imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $this->image_dst_x - $nb + $i, $nb - $i, $c); imageline($tmp, $this->image_dst_x - $nb + $i, $this->image_dst_y - $nb + $i, $nb - $i, $this->image_dst_y - $nb + $i, $c); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c); } } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add bevel border if ($this->image_bevel > 0) { if (empty($this->image_bevel_color1)) { $this->image_bevel_color1 = '#FFFFFF'; } if (empty($this->image_bevel_color2)) { $this->image_bevel_color2 = '#000000'; } list($red1, $green1, $blue1) = $this->getcolors($this->image_bevel_color1); list($red2, $green2, $blue2) = $this->getcolors($this->image_bevel_color2); $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y); imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y); imagealphablending($tmp, true); for ($i = 0; $i < $this->image_bevel; $i++) { $alpha = round($i / $this->image_bevel * 127); $c1 = imagecolorallocatealpha($tmp, $red1, $green1, $blue1, $alpha); $c2 = imagecolorallocatealpha($tmp, $red2, $green2, $blue2, $alpha); imageline($tmp, $i, $i, $this->image_dst_x - $i - 1, $i, $c1); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i, $this->image_dst_x - $i - 1, $i, $c2); imageline($tmp, $this->image_dst_x - $i - 1, $this->image_dst_y - $i - 1, $i, $this->image_dst_y - $i - 1, $c2); imageline($tmp, $i, $i, $i, $this->image_dst_y - $i - 1, $c1); } // we transfert tmp into image_dst $image_dst = $this->imagetransfer($tmp, $image_dst); } // add watermark image if ($this->image_watermark != '' && file_exists($this->image_watermark)) { $this->log .= '- add watermark<br />'; $this->image_watermark_position = strtolower($this->image_watermark_position); $watermark_info = getimagesize($this->image_watermark); $watermark_type = array_key_exists(2, $watermark_info) ? $watermark_info[2] : null; // 1 = GIF, 2 = JPG, 3 = PNG $watermark_checked = false; if ($watermark_type == IMAGETYPE_GIF) { if (!function_exists('imagecreatefromgif')) { $this->error = $this->translate('watermark_no_create_support', array('GIF')); } else { $filter = @imagecreatefromgif($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('GIF')); } else { $this->log .= ' watermark source image is GIF<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_JPEG) { if (!function_exists('imagecreatefromjpeg')) { $this->error = $this->translate('watermark_no_create_support', array('JPEG')); } else { $filter = @imagecreatefromjpeg($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('JPEG')); } else { $this->log .= ' watermark source image is JPEG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_PNG) { if (!function_exists('imagecreatefrompng')) { $this->error = $this->translate('watermark_no_create_support', array('PNG')); } else { $filter = @imagecreatefrompng($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('PNG')); } else { $this->log .= ' watermark source image is PNG<br />'; $watermark_checked = true; } } } else { if ($watermark_type == IMAGETYPE_BMP) { if (!method_exists($this, 'imagecreatefrombmp')) { $this->error = $this->translate('watermark_no_create_support', array('BMP')); } else { $filter = @$this->imagecreatefrombmp($this->image_watermark); if (!$filter) { $this->error = $this->translate('watermark_create_error', array('BMP')); } else { $this->log .= ' watermark source image is BMP<br />'; $watermark_checked = true; } } } else { $this->error = $this->translate('watermark_invalid'); } } } } if ($watermark_checked) { $watermark_dst_width = $watermark_src_width = imagesx($filter); $watermark_dst_height = $watermark_src_height = imagesy($filter); // if watermark is too large/tall, resize it first if (!$this->image_watermark_no_zoom_out && ($watermark_dst_width > $this->image_dst_x || $watermark_dst_height > $this->image_dst_y) || !$this->image_watermark_no_zoom_in && $watermark_dst_width < $this->image_dst_x && $watermark_dst_height < $this->image_dst_y) { $canvas_width = $this->image_dst_x - abs($this->image_watermark_x); $canvas_height = $this->image_dst_y - abs($this->image_watermark_y); if ($watermark_src_width / $canvas_width > $watermark_src_height / $canvas_height) { $watermark_dst_width = $canvas_width; $watermark_dst_height = intval($watermark_src_height * ($canvas_width / $watermark_src_width)); } else { $watermark_dst_height = $canvas_height; $watermark_dst_width = intval($watermark_src_width * ($canvas_height / $watermark_src_height)); } $this->log .= ' watermark resized from ' . $watermark_src_width . 'x' . $watermark_src_height . ' to ' . $watermark_dst_width . 'x' . $watermark_dst_height . '<br />'; } // determine watermark position $watermark_x = 0; $watermark_y = 0; if (is_numeric($this->image_watermark_x)) { if ($this->image_watermark_x < 0) { $watermark_x = $this->image_dst_x - $watermark_dst_width + $this->image_watermark_x; } else { $watermark_x = $this->image_watermark_x; } } else { if (strpos($this->image_watermark_position, 'r') !== false) { $watermark_x = $this->image_dst_x - $watermark_dst_width; } else { if (strpos($this->image_watermark_position, 'l') !== false) { $watermark_x = 0; } else { $watermark_x = ($this->image_dst_x - $watermark_dst_width) / 2; } } } if (is_numeric($this->image_watermark_y)) { if ($this->image_watermark_y < 0) { $watermark_y = $this->image_dst_y - $watermark_dst_height + $this->image_watermark_y; } else { $watermark_y = $this->image_watermark_y; } } else { if (strpos($this->image_watermark_position, 'b') !== false) { $watermark_y = $this->image_dst_y - $watermark_dst_height; } else { if (strpos($this->image_watermark_position, 't') !== false) { $watermark_y = 0; } else { $watermark_y = ($this->image_dst_y - $watermark_dst_height) / 2; } } } imagealphablending($image_dst, true); imagecopyresampled($image_dst, $filter, $watermark_x, $watermark_y, 0, 0, $watermark_dst_width, $watermark_dst_height, $watermark_src_width, $watermark_src_height); } else { $this->error = $this->translate('watermark_invalid'); } } // add text if (!empty($this->image_text)) { $this->log .= '- add text<br />'; // calculate sizes in human readable format $src_size = $this->file_src_size / 1024; $src_size_mb = number_format($src_size / 1024, 1, ".", " "); $src_size_kb = number_format($src_size, 1, ".", " "); $src_size_human = $src_size > 1024 ? $src_size_mb . " MB" : $src_size_kb . " kb"; $this->image_text = str_replace(array('[src_name]', '[src_name_body]', '[src_name_ext]', '[src_pathname]', '[src_mime]', '[src_size]', '[src_size_kb]', '[src_size_mb]', '[src_size_human]', '[src_x]', '[src_y]', '[src_pixels]', '[src_type]', '[src_bits]', '[dst_path]', '[dst_name_body]', '[dst_name_ext]', '[dst_name]', '[dst_pathname]', '[dst_x]', '[dst_y]', '[date]', '[time]', '[host]', '[server]', '[ip]', '[gd_version]'), array($this->file_src_name, $this->file_src_name_body, $this->file_src_name_ext, $this->file_src_pathname, $this->file_src_mime, $this->file_src_size, $src_size_kb, $src_size_mb, $src_size_human, $this->image_src_x, $this->image_src_y, $this->image_src_pixels, $this->image_src_type, $this->image_src_bits, $this->file_dst_path, $this->file_dst_name_body, $this->file_dst_name_ext, $this->file_dst_name, $this->file_dst_pathname, $this->image_dst_x, $this->image_dst_y, date('Y-m-d'), date('H:i:s'), isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'n/a', isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'n/a', isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'n/a', $this->gdversion(true)), $this->image_text); if (!is_numeric($this->image_text_padding)) { $this->image_text_padding = 0; } if (!is_numeric($this->image_text_line_spacing)) { $this->image_text_line_spacing = 0; } if (!is_numeric($this->image_text_padding_x)) { $this->image_text_padding_x = $this->image_text_padding; } if (!is_numeric($this->image_text_padding_y)) { $this->image_text_padding_y = $this->image_text_padding; } $this->image_text_position = strtolower($this->image_text_position); $this->image_text_direction = strtolower($this->image_text_direction); $this->image_text_alignment = strtolower($this->image_text_alignment); // if the font is a string, we assume that we might want to load a font if (!is_numeric($this->image_text_font) && strlen($this->image_text_font) > 4 && substr(strtolower($this->image_text_font), -4) == '.gdf') { $this->log .= ' try to load font ' . $this->image_text_font . '... '; if ($this->image_text_font = @imageloadfont($this->image_text_font)) { $this->log .= 'success<br />'; } else { $this->log .= 'error<br />'; $this->image_text_font = 5; } } $text = explode("\n", $this->image_text); $char_width = imagefontwidth($this->image_text_font); $char_height = imagefontheight($this->image_text_font); $text_height = 0; $text_width = 0; $line_height = 0; $line_width = 0; foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { $h = $char_width * strlen($v); if ($h > $text_height) { $text_height = $h; } $line_width = $char_height; $text_width += $line_width + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } else { $w = $char_width * strlen($v); if ($w > $text_width) { $text_width = $w; } $line_height = $char_height; $text_height += $line_height + ($k < sizeof($text) - 1 ? $this->image_text_line_spacing : 0); } } $text_width += 2 * $this->image_text_padding_x; $text_height += 2 * $this->image_text_padding_y; $text_x = 0; $text_y = 0; if (is_numeric($this->image_text_x)) { if ($this->image_text_x < 0) { $text_x = $this->image_dst_x - $text_width + $this->image_text_x; } else { $text_x = $this->image_text_x; } } else { if (strpos($this->image_text_position, 'r') !== false) { $text_x = $this->image_dst_x - $text_width; } else { if (strpos($this->image_text_position, 'l') !== false) { $text_x = 0; } else { $text_x = ($this->image_dst_x - $text_width) / 2; } } } if (is_numeric($this->image_text_y)) { if ($this->image_text_y < 0) { $text_y = $this->image_dst_y - $text_height + $this->image_text_y; } else { $text_y = $this->image_text_y; } } else { if (strpos($this->image_text_position, 'b') !== false) { $text_y = $this->image_dst_y - $text_height; } else { if (strpos($this->image_text_position, 't') !== false) { $text_y = 0; } else { $text_y = ($this->image_dst_y - $text_height) / 2; } } } // add a background, maybe transparent if (!empty($this->image_text_background)) { list($red, $green, $blue) = $this->getcolors($this->image_text_background); if ($gd_version >= 2 && is_numeric($this->image_text_background_percent) && $this->image_text_background_percent >= 0 && $this->image_text_background_percent <= 100) { $filter = imagecreatetruecolor($text_width, $text_height); $background_color = imagecolorallocate($filter, $red, $green, $blue); imagefilledrectangle($filter, 0, 0, $text_width, $text_height, $background_color); $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $text_width, $text_height, $this->image_text_background_percent); imagedestroy($filter); } else { $background_color = imagecolorallocate($image_dst, $red, $green, $blue); imagefilledrectangle($image_dst, $text_x, $text_y, $text_x + $text_width, $text_y + $text_height, $background_color); } } $text_x += $this->image_text_padding_x; $text_y += $this->image_text_padding_y; $t_width = $text_width - 2 * $this->image_text_padding_x; $t_height = $text_height - 2 * $this->image_text_padding_y; list($red, $green, $blue) = $this->getcolors($this->image_text_color); // add the text, maybe transparent if ($gd_version >= 2 && is_numeric($this->image_text_percent) && $this->image_text_percent >= 0 && $this->image_text_percent <= 100) { if ($t_width < 0) { $t_width = 0; } if ($t_height < 0) { $t_height = 0; } $filter = $this->imagecreatenew($t_width, $t_height, false, true); $text_color = imagecolorallocate($filter, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($filter, $this->image_text_font, $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($filter, $this->image_text_font, $this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2), $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } $this->imagecopymergealpha($image_dst, $filter, $text_x, $text_y, 0, 0, $t_width, $t_height, $this->image_text_percent); imagedestroy($filter); } else { $text_color = imageColorAllocate($image_dst, $red, $green, $blue); foreach ($text as $k => $v) { if ($this->image_text_direction == 'v') { imagestringup($image_dst, $this->image_text_font, $text_x + $k * ($line_width + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $text_y + $text_height - 2 * $this->image_text_padding_y - ($this->image_text_alignment == 'l' ? 0 : ($t_height - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $v, $text_color); } else { imagestring($image_dst, $this->image_text_font, $text_x + ($this->image_text_alignment == 'l' ? 0 : ($t_width - strlen($v) * $char_width) / ($this->image_text_alignment == 'r' ? 1 : 2)), $text_y + $k * ($line_height + ($k > 0 && $k < sizeof($text) ? $this->image_text_line_spacing : 0)), $v, $text_color); } } } } // add a reflection if ($this->image_reflection_height) { $this->log .= '- add reflection : ' . $this->image_reflection_height . '<br />'; // we decode image_reflection_height, which can be a integer, a string in pixels or percentage $image_reflection_height = $this->image_reflection_height; if (strpos($image_reflection_height, '%') > 0) { $image_reflection_height = $this->image_dst_y * str_replace('%', '', $image_reflection_height / 100); } if (strpos($image_reflection_height, 'px') > 0) { $image_reflection_height = str_replace('px', '', $image_reflection_height); } $image_reflection_height = (int) $image_reflection_height; if ($image_reflection_height > $this->image_dst_y) { $image_reflection_height = $this->image_dst_y; } if (empty($this->image_reflection_opacity)) { $this->image_reflection_opacity = 60; } // create the new destination image $tmp = $this->imagecreatenew($this->image_dst_x, $this->image_dst_y + $image_reflection_height + $this->image_reflection_space, true); $transparency = $this->image_reflection_opacity; // copy the original image imagecopy($tmp, $image_dst, 0, 0, 0, 0, $this->image_dst_x, $this->image_dst_y + ($this->image_reflection_space < 0 ? $this->image_reflection_space : 0)); // 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)) { list($red, $green, $blue) = $this->getcolors($this->image_background_color); $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']; } } list($red, $green, $blue) = $this->getcolors($this->image_default_color); // 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); } } } // transforms the true color image into palette, with its merged default color 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 />'; list($red, $green, $blue) = $this->getcolors($this->image_default_color); $transparency = imagecolorallocate($image_dst, $red, $green, $blue); // make the transaparent 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 some transparency, in which case we will merge the colors if (imageistruecolor($image_dst)) { $rgba = imagecolorat($image_dst, $x, $y); $pixel = array('red' => $rgba >> 16 & 0xff, 'green' => $rgba >> 8 & 0xff, 'blue' => $rgba & 0xff, 'alpha' => ($rgba & 0x7f000000) >> 24); } else { $pixel = imagecolorsforindex($image_dst, imagecolorat($image_dst, $x, $y)); } if ($pixel['alpha'] == 127) { // we have full transparency. we make the pixel transparent imagesetpixel($image_dst, $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($image_dst, $pixel['red'], $pixel['green'], $pixel['blue']); imagesetpixel($image_dst, $x, $y, $color); } } } } break; default: break; } // outputs image $this->log .= '- saving image...<br />'; switch ($this->image_convert) { case 'jpeg': case 'jpg': if (!$return_mode) { $result = @imagejpeg($image_dst, $this->file_dst_pathname, $this->jpeg_quality); } else { ob_start(); $result = @imagejpeg($image_dst, '', $this->jpeg_quality); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('JPEG')); } else { $this->log .= ' JPEG image created<br />'; } break; case 'png': imagealphablending($image_dst, false); imagesavealpha($image_dst, true); if (!$return_mode) { $result = @imagepng($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagepng($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('PNG')); } else { $this->log .= ' PNG image created<br />'; } break; case 'gif': if (!$return_mode) { $result = @imagegif($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = @imagegif($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('GIF')); } else { $this->log .= ' GIF image created<br />'; } break; case 'bmp': if (!$return_mode) { $result = $this->imagebmp($image_dst, $this->file_dst_pathname); } else { ob_start(); $result = $this->imagebmp($image_dst); $return_content = ob_get_contents(); ob_end_clean(); } if (!$result) { $this->processed = false; $this->error = $this->translate('file_create', array('BMP')); } else { $this->log .= ' BMP image created<br />'; } break; default: $this->processed = false; $this->error = $this->translate('no_conversion_type'); } if ($this->processed) { if (is_resource($image_src)) { imagedestroy($image_src); } if (is_resource($image_dst)) { imagedestroy($image_dst); } $this->log .= ' image objects destroyed<br />'; } } } else { $this->log .= '- no image processing wanted<br />'; if (!$return_mode) { // copy the file to its final destination. we don't use move_uploaded_file here // if we happen to have open_basedir restrictions, it is a temp file that we copy, not the original uploaded file if (!copy($this->file_src_pathname, $this->file_dst_pathname)) { $this->processed = false; $this->error = $this->translate('copy_failed'); } } else { // returns the file, so that its content can be received by the caller $return_content = @file_get_contents($this->file_src_pathname); if ($return_content === FALSE) { $this->processed = false; $this->error = $this->translate('reading_failed'); } } } } if ($this->processed) { $this->log .= '- <b>process OK</b><br />'; } else { $this->log .= '- <b>error</b>: ' . $this->error . '<br />'; } // we reinit all the vars $this->init(); // we may return the image content if ($return_mode) { return $return_content; } }
/** * Sharpens the image * @return SLIRImageLibrary * @since 2.0 */ public function sharpen() { if ($this->isSharpeningDesired()) { imageconvolution($this->getImage(), $this->sharpenMatrix($this->getSharpeningFactor()), $this->getSharpeningFactor(), 0); } return $this; }
list($w, $h) = getimagesize($saveto); $max = 100; $tw = $w; $th = $h; if ($w > $h && $max < $w) { $th = $max / $w * $h; $tw = $max; } elseif ($h > $w && $max < $h) { $tw = $max / $h * $w; $th = $max; } elseif ($max < $w) { $tw = $th = $max; } $tmp = imagecreatetruecolor($tw, $th); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); imageconvolution($tmp, array(array('−1', '−1', '−1'), array('−1', '16', '−1'), array('−1', '−1', '−1')), '8', '0'); imagejpeg($tmp, $saveto); imagedestroy($tmp); imagedestroy($src); } } showProfile($conn, $user, $fname); echo <<<_END </div> <div class='right_panel'> <form method='post' action='profile.php' enctype='multipart/form-data'> <h3>Enter or edit your details and/or upload an image</h3> <textarea name='text' cols='50' rows='3'>{$text}</textarea><br /> _END; ?> Image: <input type='file' name='image' size='14' maxlength='32' />
protected function processImageAndWriteToCache($localImage) { $sData = getimagesize($localImage); $origType = $sData[2]; $mimeType = $sData['mime']; $this->debug(3, "Mime type of image is {$mimeType}"); if (!preg_match('/^image\\/(?:gif|jpg|jpeg|png)$/i', $mimeType)) { return $this->error("The image being resized is not a valid gif, jpg or png."); } if (!function_exists('imagecreatetruecolor')) { return $this->error('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library'); } if (function_exists('imagefilter') && defined('IMG_FILTER_NEGATE')) { $imageFilters = array(1 => array(IMG_FILTER_NEGATE, 0), 2 => array(IMG_FILTER_GRAYSCALE, 0), 3 => array(IMG_FILTER_BRIGHTNESS, 1), 4 => array(IMG_FILTER_CONTRAST, 1), 5 => array(IMG_FILTER_COLORIZE, 4), 6 => array(IMG_FILTER_EDGEDETECT, 0), 7 => array(IMG_FILTER_EMBOSS, 0), 8 => array(IMG_FILTER_GAUSSIAN_BLUR, 0), 9 => array(IMG_FILTER_SELECTIVE_BLUR, 0), 10 => array(IMG_FILTER_MEAN_REMOVAL, 0), 11 => array(IMG_FILTER_SMOOTH, 0)); } // get standard input properties $new_width = (int) abs($this->param('w', 0)); $new_height = (int) abs($this->param('h', 0)); $zoom_crop = (int) $this->param('zc', DEFAULT_ZC); $quality = (int) abs($this->param('q', DEFAULT_Q)); $align = $this->cropTop ? 't' : $this->param('a', 'c'); $filters = $this->param('f', DEFAULT_F); $sharpen = (bool) $this->param('s', DEFAULT_S); $canvas_color = $this->param('cc', DEFAULT_CC); $canvas_trans = (bool) $this->param('ct', '1'); // set default width and height if neither are set already if ($new_width == 0 && $new_height == 0) { $new_width = (int) DEFAULT_WIDTH; $new_height = (int) DEFAULT_HEIGHT; } // ensure size limits can not be abused $new_width = min($new_width, MAX_WIDTH); $new_height = min($new_height, MAX_HEIGHT); // set memory limit to be able to have enough space to resize larger images $this->setMemoryLimit(); // open the existing image $image = $this->openImage($mimeType, $localImage); if ($image === false) { return $this->error('Unable to open image.'); } // Get original width and height $width = imagesx($image); $height = imagesy($image); $origin_x = 0; $origin_y = 0; // generate new w/h if not provided if ($new_width && !$new_height) { $new_height = floor($height * ($new_width / $width)); } else { if ($new_height && !$new_width) { $new_width = floor($width * ($new_height / $height)); } } // scale down and add borders if ($zoom_crop == 3) { $final_height = $height * ($new_width / $width); if ($final_height > $new_height) { $new_width = $width * ($new_height / $height); } else { $new_height = $final_height; } } // create a new true color image $canvas = imagecreatetruecolor($new_width, $new_height); imagealphablending($canvas, false); if (strlen($canvas_color) == 3) { //if is 3-char notation, edit string into 6-char notation $canvas_color = str_repeat(substr($canvas_color, 0, 1), 2) . str_repeat(substr($canvas_color, 1, 1), 2) . str_repeat(substr($canvas_color, 2, 1), 2); } else { if (strlen($canvas_color) != 6) { $canvas_color = DEFAULT_CC; // on error return default canvas color } } $canvas_color_R = hexdec(substr($canvas_color, 0, 2)); $canvas_color_G = hexdec(substr($canvas_color, 2, 2)); $canvas_color_B = hexdec(substr($canvas_color, 4, 2)); // Create a new transparent color for image // If is a png and PNG_IS_TRANSPARENT is false then remove the alpha transparency // (and if is set a canvas color show it in the background) if (preg_match('/^image\\/png$/i', $mimeType) && !PNG_IS_TRANSPARENT && $canvas_trans) { $color = imagecolorallocatealpha($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 127); } else { $color = imagecolorallocatealpha($canvas, $canvas_color_R, $canvas_color_G, $canvas_color_B, 0); } // Completely fill the background of the new image with allocated color. imagefill($canvas, 0, 0, $color); // scale down and add borders if ($zoom_crop == 2) { $final_height = $height * ($new_width / $width); if ($final_height > $new_height) { $origin_x = $new_width / 2; $new_width = $width * ($new_height / $height); $origin_x = round($origin_x - $new_width / 2); } else { $origin_y = $new_height / 2; $new_height = $final_height; $origin_y = round($origin_y - $new_height / 2); } } // Restore transparency blending imagesavealpha($canvas, true); if ($zoom_crop > 0) { $src_x = $src_y = 0; $src_w = $width; $src_h = $height; $cmp_x = $width / $new_width; $cmp_y = $height / $new_height; // calculate x or y coordinate and width or height of source if ($cmp_x > $cmp_y) { $src_w = round($width / $cmp_x * $cmp_y); $src_x = round(($width - $width / $cmp_x * $cmp_y) / 2); } else { if ($cmp_y > $cmp_x) { $src_h = round($height / $cmp_y * $cmp_x); $src_y = round(($height - $height / $cmp_y * $cmp_x) / 2); } } // positional cropping! if ($align) { if (strpos($align, 't') !== false) { $src_y = 0; } if (strpos($align, 'b') !== false) { $src_y = $height - $src_h; } if (strpos($align, 'l') !== false) { $src_x = 0; } if (strpos($align, 'r') !== false) { $src_x = $width - $src_w; } } imagecopyresampled($canvas, $image, $origin_x, $origin_y, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h); } else { // copy and resize part of an image with resampling imagecopyresampled($canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } if ($filters != '' && function_exists('imagefilter') && defined('IMG_FILTER_NEGATE')) { // apply filters to image $filterList = explode('|', $filters); foreach ($filterList as $fl) { $filterSettings = explode(',', $fl); if (isset($imageFilters[$filterSettings[0]])) { for ($i = 0; $i < 4; $i++) { if (!isset($filterSettings[$i])) { $filterSettings[$i] = null; } else { $filterSettings[$i] = (int) $filterSettings[$i]; } } switch ($imageFilters[$filterSettings[0]][1]) { case 1: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]); break; case 2: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]); break; case 3: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]); break; case 4: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3], $filterSettings[4]); break; default: imagefilter($canvas, $imageFilters[$filterSettings[0]][0]); break; } } } } // sharpen image if ($sharpen && function_exists('imageconvolution')) { $sharpenMatrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)); $divisor = 8; $offset = 0; imageconvolution($canvas, $sharpenMatrix, $divisor, $offset); } //Straight from Wordpress core code. Reduces filesize by up to 70% for PNG's if ((IMAGETYPE_PNG == $origType || IMAGETYPE_GIF == $origType) && function_exists('imageistruecolor') && !imageistruecolor($image) && imagecolortransparent($image) > 0) { imagetruecolortopalette($canvas, false, imagecolorstotal($image)); } $imgType = ""; $tempfile = tempnam($this->cacheDirectory, 'timthumb_tmpimg_'); if (preg_match('/^image\\/(?:jpg|jpeg)$/i', $mimeType)) { $imgType = 'jpg'; imagejpeg($canvas, $tempfile, $quality); } else { if (preg_match('/^image\\/png$/i', $mimeType)) { $imgType = 'png'; imagepng($canvas, $tempfile, floor($quality * 0.09)); } else { if (preg_match('/^image\\/gif$/i', $mimeType)) { $imgType = 'gif'; imagegif($canvas, $tempfile); } else { return $this->sanityFail("Could not match mime type after verifying it previously."); } } } if ($imgType == 'png' && OPTIPNG_ENABLED && OPTIPNG_PATH && @is_file(OPTIPNG_PATH)) { $exec = OPTIPNG_PATH; $this->debug(3, "optipng'ing {$tempfile}"); $presize = filesize($tempfile); $out = `{$exec} -o1 {$tempfile}`; //you can use up to -o7 but it really slows things down clearstatcache(); $aftersize = filesize($tempfile); $sizeDrop = $presize - $aftersize; if ($sizeDrop > 0) { $this->debug(1, "optipng reduced size by {$sizeDrop}"); } else { if ($sizeDrop < 0) { $this->debug(1, "optipng increased size! Difference was: {$sizeDrop}"); } else { $this->debug(1, "optipng did not change image size."); } } } else { if ($imgType == 'png' && PNGCRUSH_ENABLED && PNGCRUSH_PATH && @is_file(PNGCRUSH_PATH)) { $exec = PNGCRUSH_PATH; $tempfile2 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_'); $this->debug(3, "pngcrush'ing {$tempfile} to {$tempfile2}"); $out = `{$exec} {$tempfile} {$tempfile2}`; $todel = ""; if (is_file($tempfile2)) { $sizeDrop = filesize($tempfile) - filesize($tempfile2); if ($sizeDrop > 0) { $this->debug(1, "pngcrush was succesful and gave a {$sizeDrop} byte size reduction"); $todel = $tempfile; $tempfile = $tempfile2; } else { $this->debug(1, "pngcrush did not reduce file size. Difference was {$sizeDrop} bytes."); $todel = $tempfile2; } } else { $this->debug(3, "pngcrush failed with output: {$out}"); $todel = $tempfile2; } @unlink($todel); } } $this->debug(3, "Rewriting image with security header."); $tempfile4 = tempnam($this->cacheDirectory, 'timthumb_tmpimg_'); $context = stream_context_create(); $fp = fopen($tempfile, 'r', 0, $context); file_put_contents($tempfile4, $this->filePrependSecurityBlock . $imgType . ' ?' . '>'); //6 extra bytes, first 3 being image type file_put_contents($tempfile4, $fp, FILE_APPEND); fclose($fp); @unlink($tempfile); $this->debug(3, "Locking and replacing cache file."); $lockFile = $this->cachefile . '.lock'; $fh = fopen($lockFile, 'w'); if (!$fh) { return $this->error("Could not open the lockfile for writing an image."); } if (flock($fh, LOCK_EX)) { @unlink($this->cachefile); //rename generally overwrites, but doing this in case of platform specific quirks. File might not exist yet. rename($tempfile4, $this->cachefile); flock($fh, LOCK_UN); fclose($fh); @unlink($lockFile); } else { fclose($fh); @unlink($lockFile); @unlink($tempfile4); return $this->error("Could not get a lock for writing."); } $this->debug(3, "Done image replace with security header. Cleaning up and running cleanCache()"); imagedestroy($canvas); imagedestroy($image); return true; }
/** * Sharpens the image * * @param integer $sharpness * @since 2.0 */ public final function sharpen($sharpness) { if ($this->isSharpeningDesired()) { imageconvolution($this->image, $this->sharpenMatrix($sharpness), $sharpness, 0); } }