function MaxFileSize() { if (phpthumb_functions::gd_version() < 2) { $this->DebugMessage('Skipping MaxFileSize() because gd_version is "' . phpthumb_functions::gd_version() . '"', __FILE__, __LINE__); return false; } if ($this->maxb > 0) { switch ($this->thumbnailFormat) { case 'png': case 'gif': $imgRenderFunction = 'image' . $this->thumbnailFormat; ob_start(); $imgRenderFunction($this->gdimg_output); $imgdata = ob_get_contents(); ob_end_clean(); if (strlen($imgdata) > $this->maxb) { for ($i = 8; $i >= 1; $i--) { $tempIMG = ImageCreateTrueColor(ImageSX($this->gdimg_output), ImageSY($this->gdimg_output)); ImageCopy($tempIMG, $this->gdimg_output, 0, 0, 0, 0, ImageSX($this->gdimg_output), ImageSY($this->gdimg_output)); ImageTrueColorToPalette($tempIMG, true, pow(2, $i)); ob_start(); $imgRenderFunction($tempIMG); $imgdata = ob_get_contents(); ob_end_clean(); if (strlen($imgdata) <= $this->maxb) { ImageTrueColorToPalette($this->gdimg_output, true, pow(2, $i)); break; } } } if (strlen($imgdata) > $this->maxb) { ImageTrueColorToPalette($this->gdimg_output, true, pow(2, $i)); return false; } break; case 'jpeg': ob_start(); ImageJPEG($this->gdimg_output); $imgdata = ob_get_contents(); ob_end_clean(); $OriginalJPEGquality = $this->thumbnailQuality; if (strlen($imgdata) > $this->maxb) { for ($i = 3; $i < 20; $i++) { $q = round(100 * (1 - log10($i / 2))); ob_start(); ImageJPEG($this->gdimg_output, '', $q); $imgdata = ob_get_contents(); ob_end_clean(); $this->thumbnailQuality = $q; if (strlen($imgdata) <= $this->maxb) { break; } } } if (strlen($imgdata) > $this->maxb) { return false; } break; default: return false; break; } } return true; }
public function ImageTrueColorToPalette2(&$image, $dither, $ncolors) { // http://www.php.net/manual/en/function.imagetruecolortopalette.php // zmorris at zsculpt dot com (17-Aug-2004 06:58) $width = ImageSX($image); $height = ImageSY($image); $image_copy = ImageCreateTrueColor($width, $height); //ImageCopyMerge($image_copy, $image, 0, 0, 0, 0, $width, $height, 100); ImageCopy($image_copy, $image, 0, 0, 0, 0, $width, $height); ImageTrueColorToPalette($image, $dither, $ncolors); ImageColorMatch($image_copy, $image); ImageDestroy($image_copy); return true; }
function colorDecrease($image, $dither, $ncolors) { $width = imagesx($image); $height = imagesy($image); $colors_handle = ImageCreateTrueColor($width, $height); ImageCopyMerge($colors_handle, $image, 0, 0, 0, 0, $width, $height, 100); ImageTrueColorToPalette($image, $dither, $ncolors); ImageColorMatch($colors_handle, $image); ImageDestroy($colors_handle); }
function init_wanted($id_troll) { $lesTrolls = selectDbTrolls($id_troll); $res = $lesTrolls[1]; $lesVengeances = selectDbVengeances($id_troll); $nb_vengeances = count($lesVengeances); $lesGriefs = selectDbGriefs($id_troll); $nb_griefs = count($lesGriefs); $impact = PATH_IMG . "wanted_impact_{$nb_vengeances}.png"; if ($res[nom_image_troll] != "") { $fp = @fopen($res["nom_image_troll"], "r"); } else { $fp = false; } /* print_r($res); die("test $res[nom_image_troll]");*/ $images = array(ImageCreateFromPng(PATH_IMG . "wanted_fond.png"), ImageCreateFromPng(PATH_IMG . "wanted_fond.png"), ImageCreateFromPng(PATH_IMG . "wanted_transparent.png"), ImageCreateFromPng($impact), ImageCreateFromPng(PATH_IMG . "wanted_grief_{$nb_griefs}.png")); if ($fp == false) { $images[2] = ImageCreateFromPng(PATH_IMG . "wanted_interrogation.png"); } elseif (preg_match("/.*(png|Png|PNG)/", $res["nom_image_troll"])) { $img = ImageCreateFromJpeg($res["nom_image_troll"]); } elseif (preg_match("/.*(jpg|jpeg|JPG|JPEG)/", $res["nom_image_troll"])) { $img = ImageCreateFromJpeg($res["nom_image_troll"]); } elseif (preg_match("/.*(gif|Gif|GIF)/", $res["nom_image_troll"])) { $img = ImageCreateFromGif($res["nom_image_troll"]); } elseif (preg_match("/.*mountyhall.*Blason_PJ*./", $res["nom_image_troll"])) { $img = @ImageCreateFromGif($res["nom_image_troll"]); if (!$img) { $img = @ImageCreateFromJpeg($res["nom_image_troll"]); if (!$img) { $img = @ImageCreateFromPng($res["nom_image_troll"]); if (!$img) { $fp = false; } } } } else { $images[2] = ImageCreateFromPng(PATH_IMG . "wanted_interrogation.png"); fclose($fp); $fp = false; } if ($fp != false) { $img_src_w = imagesx($img); $img_src_h = imagesy($img); $img_dest_w = 43; //150 75 33 $img_dest_h = 55; // 231 115 55 imagecopyresized($images[2], $img, 30, 44, 0, 0, $img_dest_w, $img_dest_h, $img_src_w, $img_src_h); fclose($fp); } $image = imagemergealpha($images); /* -------- Destruction ------- */ foreach ($images as $img) { ImageDestroy($img); } putenv('GDFONTPATH=' . realpath('.')); $font = "Wanted"; $font_size = 5; $noir = ImageColorAllocate($image, 0, 0, 0); //imagettftext($image, $font_size, 0, 10, 38, $noir, $font, $res['nom_troll']); imagestring($image, 2, 10, 30, $res['nom_troll'], $noir); //imagettftext($image, $font_size, 0, 10, 38, $noir, $font, $res['nom_troll']); /* -------- Génération ------- */ /* ImagePng($image,PATH_IMG.'cache/'.$res[nom_image_troll]."_avatar.png"); */ /* -------- Cache -------- */ $fondclair = ImageCreateFromPng(PATH_IMG . 'wanted_fond.png'); $gifclair = imagemergealpha(array($fondclair, $image)); $back_color = imagecolorallocate($gifclair, 0xa9, 0xb1, 0xd3); ImageTrueColorToPalette($gifclair, false, 256); ImageGif($gifclair, PATH_IMG . "cache/wanted_{$id_troll}.gif"); /* -------- Affichage -------- */ //Header('Content-Type: image/gif'); // BUG PHP5 //ImageGif($gifclair); $string = file_get_contents(PATH_IMG . "cache/wanted_{$id_troll}.gif"); echo $string; ImageDestroy($fondclair); ImageDestroy($gifclair); }
/** * resizes an image using several different techniques: * * PHP's own ImageCopyResamplated * Bi-linear filter (slower, but better quality than ImageCopyResampled) * Bi-Cubic filter (slowest, but offers the best quality) * PHP's own ImageCopyResized (fastest one, but offers no antialising or filter) * */ function ImageResize($dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $resample = GD_RESIZER_NO_SMOOTHING_MODE) { $pxls = intval($src_w / $dst_w) - 1; if ($resample == GD_RESIZER_PHP_IMAGECOPYRESAMPLED) { imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } elseif ($resample == GD_RESIZER_BILINEAR_MODE) { //slow but better quality ImageTrueColorToPalette($src_img, false, 256); ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $nY = 0; for ($y = $src_y; $y < $dst_h; $y++) { $oY = $nY; $nY = intval(($y + 1) * $rY + 0.5); $nX = 0; for ($x = $src_x; $x < $dst_w; $x++) { $r = $g = $b = $a = 0; $oX = $nX; $nX = intval(($x + 1) * $rX + 0.5); $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $nX, $nY)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $nX - $pxls, $nY - $pxls)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; //you can add more pixels here! eg "$nX, $nY-$pxls" or "$nX-$pxls, $nY" ImageSetPixel($dst_img, $x + $dst_x - $src_x, $y + $dst_y - $src_y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } } elseif ($resample == GD_RESIZER_BICUBIC_MODE) { // veeeeeery slow but better quality ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $nY = 0; for ($y = $src_y; $y < $dst_h; $y++) { $oY = $nY; $nY = intval(($y + 1) * $rY + 0.5); $nX = 0; for ($x = $src_x; $x < $dst_w; $x++) { $r = $g = $b = $a = 0; $oX = $nX; $nX = intval(($x + 1) * $rX + 0.5); for ($i = $nY; --$i >= $oY;) { for ($j = $nX; --$j >= $oX;) { $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $j, $i)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; } } ImageSetPixel($dst_img, $x + $dst_x - $src_x, $y + $dst_y - $src_y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } } else { $dst_w++; $dst_h++; //->no black border imagecopyresized($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } }
private function retailler($nouvelle_largeur, $nouvelle_hauteur, $delta_largeur, $delta_hauteur, $reduite = false) { $ret = false; if (!$this->is_null()) { $src_r = null; if (!strcmp($this->get_ext(), _UPLOAD_EXTENSION_JPG) || !strcmp($this->get_ext(), _UPLOAD_EXTENSION_JPEG)) { $src_r = imagecreatefromjpeg($this->get_src()); } elseif (!strcmp($this->get_ext(), _UPLOAD_EXTENSION_PNG)) { $src_r = imagecreatefrompng($this->get_src()); } elseif (!strcmp($this->get_ext(), _UPLOAD_EXTENSION_GIF)) { $src_r = imagecreatefromgif($this->get_src()); } if ($src_r) { if ($this->get_ext() == _UPLOAD_EXTENSION_JPG || $this->get_ext() == _UPLOAD_EXTENSION_JPEG) { $dst_r = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); if ($dst_r) { imagecopyresampled($dst_r, $src_r, 0, 0, $delta_largeur, $delta_hauteur, $nouvelle_largeur, $nouvelle_hauteur, $this->get_largeur() - 2 * $delta_largeur, $this->get_hauteur() - 2 * $delta_hauteur); $qualite = $reduite ? self::qualite_jpg_reduite : self::qualite_jpg; $ret = imagejpeg($dst_r, $this->get_src(), $qualite); // Mise à jour des nouvelles dimensions $this->largeur = $nouvelle_largeur; $this->hauteur = $nouvelle_hauteur; imagedestroy($dst_r); } } elseif ($this->get_ext() == _UPLOAD_EXTENSION_PNG) { $src_alpha = $this->png_has_transparency($this->get_src()); $dst_r = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); if ($dst_r) { if ($src_alpha) { imagealphablending($dst_r, false); imagesavealpha($dst_r, true); } imagecopyresampled($dst_r, $src_r, 0, 0, $delta_largeur, $delta_hauteur, $nouvelle_largeur, $nouvelle_hauteur, $this->get_largeur() - 2 * $delta_largeur, $this->get_hauteur() - 2 * $delta_hauteur); /* En cas d'image non transparente on reduit à une image avec palette (pb de taille) */ if (!$src_alpha) { $tmp = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); ImageCopyMerge($tmp, $dst_r, 0, 0, 0, 0, $nouvelle_largeur, $nouvelle_hauteur, 100); ImageTrueColorToPalette($dst_r, false, 8192); ImageColorMatch($tmp, $dst_r); ImageDestroy($tmp); } $qualite = $reduite ? self::qualite_png_reduite : self::qualite_png; $ret = imagepng($dst_r, $this->get_src(), $qualite); // Mise à jour des nouvelles dimensions $this->largeur = $nouvelle_largeur; $this->hauteur = $nouvelle_hauteur; imagedestroy($dst_r); } } elseif ($this->get_ext() == _UPLOAD_EXTENSION_GIF) { $dst_r = ImageCreateTrueColor($nouvelle_largeur, $nouvelle_hauteur); if ($dst_r) { imagecopyresampled($dst_r, $src_r, 0, 0, $delta_largeur, $delta_hauteur, $nouvelle_largeur, $nouvelle_hauteur, $this->get_largeur() - 2 * $delta_largeur, $this->get_hauteur() - 2 * $delta_hauteur); $ret = imagegif($dst_r, $this->get_src()); // Mise à jour des nouvelles dimensions $this->largeur = $nouvelle_largeur; $this->hauteur = $nouvelle_hauteur; imagedestroy($dst_r); } } imagedestroy($src_r); } } return $ret; }
/** * Generates a resized version of an image and saves it in the image cache folder. * * @param string $source_file The original image to be resized. * @param string $cache_file The target file where the resized version will be cached. * @param int $resolution The resolution breakpoint at which the given image is to be resized. * @param int $jpg_quality The JPEG quality that will be used for resizing the images. * @param bool $sharpen Whether to sharpen the resized images or not. * * @return array Associative array( bool: success, string: message) with the result of the image cache generation. */ function adaptive_images_script_generate_image($source_file, $cache_file, $resolution, $jpg_quality, $sharpen) { // Get original image dimensions. $dimensions = @GetImageSize($source_file); $width = $dimensions[0]; $height = $dimensions[1]; // Calculate resized image dimensions. $ratio = $height / $width; $new_width = $resolution; $new_height = ceil($new_width * $ratio); // Start creating the resized image with a blank true color canvas. $destination = @ImageCreateTrueColor($new_width, $new_height); $extension = adaptive_images_script_get_file_extension($source_file); switch ($extension) { case 'png': $source = @ImageCreateFromPng($source_file); break; case 'gif': $source = @ImageCreateFromGif($source_file); break; default: $source = @ImageCreateFromJpeg($source_file); break; } // PNG images generation. if ($extension == 'png') { // Create a transparent color and fill the blank canvas with it. $rbga_color = @ImageColorAllocateAlpha($destination, 0, 0, 0, 127); @ImageColorTransparent($destination, $rbga_color); @ImageFill($destination, 0, 0, $rbga_color); // Copy source image to destination image with interpolation. @ImageCopyResampled($destination, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Convert true colour image to pallette image to achieve PNG-8 compression. $dither = TRUE; @ImageTrueColorToPalette($destination, $dither, 255); // Save alpha (transparency) of destination image. $save_alpha = TRUE; @ImageSaveAlpha($destination, $save_alpha); // Disable blending of destination image to allow for alpha (transparency) above. $enable_alpha_blending = FALSE; @ImageAlphaBlending($destination, $enable_alpha_blending); } // GIF images generation. if ($extension == 'gif') { // Create a transparent color and fill the blank canvas with it. $rbga_color = @ImageColorAllocateAlpha($destination, 0, 0, 0, 127); @ImageColorTransparent($destination, $rbga_color); @ImageFill($destination, 0, 0, $rbga_color); // Copy source image to destination image with interpolation. @ImageCopyResampled($destination, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Convert true colour image to pallette image to achieve PNG8 compression. $dither = TRUE; @ImageTrueColorToPalette($destination, $dither, 255); // Enable alpha blending of destination image. $enable_alpha_blending = TRUE; @ImageAlphaBlending($destination, $enable_alpha_blending); } // JPEG images generation. if ($extension == 'jpg' || $extension == 'jpeg') { // Enable JPEG interlacing. ImageInterlace($destination, TRUE); // Interpolates source image to destination image to make it more clear for JPGs. @ImageCopyResampled($destination, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height); } // Cleanup source image from memory. @ImageDestroy($source); // Do sharpening if requested. if ($sharpen && function_exists('imageconvolution')) { $sharpness_factor = adaptive_images_script_sharpness_factor($width, $new_width); $sharpness_transformation_matrix = array(array(-1, -2, -1), array(-2, $sharpness_factor + 12, -2), array(-1, -2, -1)); // OR // $sharpenMatrix = array // ( // array(-1.2, -1, -1.2), // array(-1, 20, -1), // array(-1.2, -1, -1.2) // ); // $divisor = array_sum(array_map('array_sum', $sharpenMatrix)); // OR // $sharpen = array( // array(0.0, -1.0, 0.0), // array(-1.0, 5.0, -1.0), // array(0.0, -1.0, 0.0) // ); // $divisor = array_sum(array_map('array_sum', $sharpen)); // OR // $matrix = array( // array(-1, -1, -1), // array(-1, 16, -1), // array(-1, -1, -1), // ); // $divisor = array_sum(array_map('array_sum', $matrix)); @ImageConvolution($destination, $sharpness_transformation_matrix, $sharpness_factor, 0); } // Check and ensure that cache directory is setup OK. $cache_path = dirname($cache_file); if (!adaptive_images_script_ensure_cache_directory_ready($cache_path)) { return array('success' => false, 'message' => 'Cache directory for image not accessible or writeable.'); } // Save resized image in cache. switch ($extension) { case 'png': $png_compression_level = 6; $image_saved = @ImagePng($destination, $cache_file, $png_compression_level, PNG_FILTER_NONE); break; case 'gif': $image_saved = @ImageGif($destination, $cache_file); break; default: $image_saved = @ImageJpeg($destination, $cache_file, $jpg_quality); break; } // Cleanup destination image from memory. @ImageDestroy($destination); // Check if all OK. if (!$image_saved && !file_exists($cache_file)) { return array('success' => false, 'message' => 'Resized image could not be created.'); } // Return file of resized and cached image. return array('success' => true, 'message' => $cache_file); }
function ImageTrueColorToPalette2($original_image, $dither, $ncolors) { $width = imagesx($original_image); $height = imagesy($original_image); $colors_handle = ImageCreateTrueColor($width, $height); imagecopymerge($colors_handle, $original_image, 0, 0, 0, 0, $width, $height, 100); ImageTrueColorToPalette($original_image, $dither, $ncolors); ImageColorMatch($colors_handle, $original_image); ImageDestroy($colors_handle); return $original_image; }
public function createThumb($dir, $prefix, $max_x = false, $max_y = false, $prefixIsRealName = false, $mode = 'square') { if (!isset($this->hash)) { return FALSE; } if (!$dir) { $dir = dirname($this->file); } if (!$prefix) { $prefix = 't'; } if (!$max_x && !$max_y || intval($max_x) <= 0 && intval($max_y) <= 0) { throw new PException('Neither thumbnail max-width nor max-height provided!'); } $size_x = $this->imageSize[0]; $size_y = $this->imageSize[1]; // old school if (!$max_x || !$max_y) { if ($max_x && intval($max_x) > 0 && $size_x) { $th_size_x = intval($max_x); $th_size_y = intval($size_y * $th_size_x / $size_x); $size_x = $th_size_x; $size_y = $th_size_y; } if ($max_y && intval($max_y) > 0 && $size_y > $max_y) { $th_size_y = intval($max_y); $th_size_x = intval($size_x * $th_size_y / $size_y); } $startx = 0; $starty = 0; $size_x = $this->imageSize[0]; $size_y = $this->imageSize[1]; } else { switch ($mode) { case "ratio": if ($max_x / $size_x >= $max_y / $size_y) { $ratio = $max_y / $size_y; } else { $ratio = $max_x / $size_x; } $startx = 0; $starty = 0; break; default: if ($size_x >= $size_y) { $startx = ($size_x - $size_y) / 2; $starty = 0; $size_x = $size_y; } else { $starty = ($size_y - $size_x) / 2; $startx = 0; $size_y = $size_x; } if ($max_x >= $max_y) { $ratio = $max_y / $size_y; } else { $ratio = $max_x / $size_x; } break; } $th_size_x = $size_x * $ratio; $th_size_y = $size_y * $ratio; } switch (intval($this->imageSize[2])) { // note: fixed this to use the proper constants. IMG_*** are GD constants, IMAGETYPE_*** are PHP constants. // The two DO NOT mix case IMAGETYPE_GIF: $oldImage = ImageCreateFromGIF($this->file); break; case IMAGETYPE_JPEG: $oldImage = ImageCreateFromJPEG($this->file); break; case IMAGETYPE_PNG: $oldImage = ImageCreateFromPNG($this->file); break; case IMAGETYPE_WBMP: $oldImage = ImageCreateFromWBMP($this->file); break; default: $e = new PException('Image type not supported!'); $e->addInfo(print_r($this->imageSize, TRUE)); throw $e; break; } $newImage = ImageCreateTrueColor($th_size_x, $th_size_y); imageCopyResampled($newImage, $oldImage, 0, 0, $startx, $starty, $th_size_x, $th_size_y, $size_x, $size_y); $newFile = tempnam('Lorem ipsum dolor sit amet', 'thumb'); switch ($this->imageSize[2]) { case IMAGETYPE_GIF: ImageTrueColorToPalette($newImage, TRUE, 256); ImageGIF($newImage, $newFile); $mimetype = 'image/gif'; break; case IMAGETYPE_JPEG: ImageJPEG($newImage, $newFile); $mimetype = 'image/jpeg'; break; case IMAGETYPE_PNG: ImagePNG($newImage, $newFile); $mimetype = 'image/png'; break; case IMAGETYPE_WBMP: ImageWBMP($newImage, $newFile); $mimetype = 'image/wbmp'; break; } $dest = $dir . '/' . $prefix; if (!$prefixIsRealName) { $dest .= $this->hash; } if (!@copy($newFile, $dest)) { return false; } unlink($newFile); return true; }
/** * Converts an image to n-colours * * @param integer $colors Number of colours in pallete * @return boolean Whether it was sucessful * @access public */ function reduce_colors($colors = 255) { //convert o,age to 256 colours if (!function_exists("ImageTrueColorToPalette")) { return false; } ImageTrueColorToPalette($this->im, 1, $colors); return true; }
private function colorDecrease() { $gifFInal = TMPDIR . "/" . $this->outWidth . "x" . $this->outHeight . "_T.gif"; if ($this->inFormat == "image/jpeg") { $src_img = ImageCreateFromJPEG($this->inPath) or die('colorDecrease: Problema leyendo jpg original'); } else { $src_img = ImageCreateFromGIF($this->inPath) or die('colorDecrease: Problema leyendo gif original'); } $dest_img = imagecreatetruecolor($this->outWidth, $this->outHeight) or die('colorDecrease: Problema en la creaci�n de la imgen nueva gif'); imagecopy($dest_img, $src_img, 0, 0, 0, 0, $this->outWidth, $this->outHeight); ImageTrueColorToPalette($dest_img, true, $this->nColors); ImageColorMatch($src_img, $dest_img); if (!imagegif($dest_img, $gifFInal)) { die("colorDecrease: Problema guardando la IMG gif en: {$outPath}"); } ImageDestroy($dest_img); $this->actualSize = filesize($gifFInal); if ($this->actualSize > $this->maxFileSize) { $this->nColors--; if (unlink($gifFInal)) { $this->colorDecrease(); } } else { unlink($this->jpgGifXcolors); } }