/** * Based on the Watermark function by Marek Malcherek * http://www.malcherek.de * * @param string $color * @param string $wmFont * @param int $wmSize * @param int $wmOpaque */ function watermarkCreateText($color = '000000', $wmFont, $wmSize = 10, $wmOpaque = 90) { // set font path $wmFontPath = NGGALLERY_ABSPATH . "fonts/" . $wmFont; if (!is_readable($wmFontPath)) { return; } // This function requires both the GD library and the FreeType library. if (!function_exists('ImageTTFBBox')) { return; } $words = preg_split('/ /', $this->watermarkText); $lines = array(); $line = ''; $watermark_image_width = 0; // attempt adding a new word until the width is too large; then start a new line and start again foreach ($words as $word) { // sanitize the text being input; imagettftext() can be sensitive $TextSize = $this->ImageTTFBBoxDimensions($wmSize, 0, $wmFontPath, $line . preg_replace('~^(&([a-zA-Z0-9]);)~', htmlentities('${1}'), mb_convert_encoding($word, "HTML-ENTITIES", "UTF-8"))); if ($watermark_image_width == 0) { $watermark_image_width = $TextSize['width']; } if ($TextSize['width'] > $this->newDimensions['newWidth']) { $lines[] = trim($line); $line = ''; } else { if ($TextSize['width'] > $watermark_image_width) { $watermark_image_width = $TextSize['width']; } } $line .= $word . ' '; } $lines[] = trim($line); // use this string to determine our largest possible line height $line_dimensions = $this->ImageTTFBBoxDimensions($wmSize, 0, $wmFontPath, 'MXQJALYmxqjabdfghjklpqry019`@$^&*(,!132'); $line_height = $line_dimensions['height'] * 1.05; // Create an image to apply our text to $this->workingImage = ImageCreateTrueColor($watermark_image_width, count($lines) * $line_height); ImageSaveAlpha($this->workingImage, true); ImageAlphaBlending($this->workingImage, false); $bgText = imagecolorallocatealpha($this->workingImage, 255, 255, 255, 127); imagefill($this->workingImage, 0, 0, $bgText); $wmTransp = 127 - $wmOpaque * 1.27; $rgb = $this->hex2rgb($color, false); $TextColor = imagecolorallocatealpha($this->workingImage, $rgb[0], $rgb[1], $rgb[2], $wmTransp); // Put text on the image, line-by-line $y_pos = $wmSize; foreach ($lines as $line) { imagettftext($this->workingImage, $wmSize, 0, 0, $y_pos, $TextColor, $wmFontPath, $line); $y_pos += $line_height; } $this->watermarkImgPath = $this->workingImage; return; }
function canvas ($width,$height,$alpha=false) { $this->processed = ImageCreateTrueColor($width,$height); if ($alpha) { ImageAlphaBlending($this->processed, false); $transparent = ImageColorAllocateAlpha($this->processed, 0, 0, 0, 127); ImageFill($this->processed, 0, 0, $transparent); ImageSaveAlpha($this->processed, true); $this->alpha = true; } }
function __construct() { header('Pragma: public'); header('Cache-Control: max-age=86400'); header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 86400)); header('Content-Type: image/png'); $this->image = ImageCreateTrueColor(96, 96); ImageSaveAlpha($this->image, true); ImageFill($this->image, 0, 0, ImageColorAllocateAlpha($this->image, 0, 0, 0, 127)); }
function cs_resample($image, $target, $max_width, $max_height) { $gd_info = gd_info(); $im_info = array(); if (file_exists($image)) { $im_info = getimagesize($image); } else { cs_error(__FILE__, 'Image file does not exist: "' . $image . '"'); return false; } if ($im_info[2] == 1 and !empty($gd_info["GIF Read Support"])) { $src = ImageCreateFromGIF($image); } elseif ($im_info[2] == 2 and (!empty($gd_info["JPG Support"]) or !empty($gd_info["JPEG Support"]))) { $src = ImageCreateFromJPEG($image); } elseif ($im_info[2] == 3 and !empty($gd_info["PNG Support"])) { $src = ImageCreateFromPNG($image); } else { cs_error(__FILE__, 'Image filetype is not supported: "' . $image . '"'); return false; } $factor = max($im_info[1] / $max_height, $im_info[0] / $max_width); $im_new[0] = floor($im_info[0] / $factor); $im_new[1] = floor($im_info[1] / $factor); $dst = ImageCreateTrueColor($im_new[0], $im_new[1]); ImageAlphaBlending($dst, false); ImageSaveAlpha($dst, true); ImageCopyResampled($dst, $src, 0, 0, 0, 0, $im_new[0], $im_new[1], $im_info[0], $im_info[1]); if ($im_info[2] == 1) { $return = ImageGIF($dst, $target) ? 1 : 0; } elseif ($im_info[2] == 2) { $return = ImageJPEG($dst, $target, 100) ? 1 : 0; } elseif ($im_info[2] == 3) { $return = ImagePNG($dst, $target) ? 1 : 0; } else { cs_error(__FILE__, 'Failed to write resampled image file: "' . $target . '"'); return false; } return $return; }
function CreateGDoutput() { $this->CalculateThumbnailDimensions(); // Create the GD image (either true-color or 256-color, depending on GD version) $this->gdimg_output = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height); // Images that have transparency must have the background filled with the configured 'bg' color // otherwise the transparent color will appear as black ImageSaveAlpha($this->gdimg_output, true); if ($this->is_alpha && phpthumb_functions::gd_version() >= 2) { ImageAlphaBlending($this->gdimg_output, false); $output_full_alpha = phpthumb_functions::ImageColorAllocateAlphaSafe($this->gdimg_output, 255, 255, 255, 127); ImageFilledRectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $output_full_alpha); } else { $current_transparent_color = ImageColorTransparent($this->gdimg_source); if ($this->bg || @$current_transparent_color >= 0) { $this->config_background_hexcolor = $this->bg ? $this->bg : $this->config_background_hexcolor; if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) { return $this->ErrorImage('Invalid hex color string "' . $this->config_background_hexcolor . '" for parameter "bg"'); } $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor); ImageFilledRectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color); } } $this->DebugMessage('CreateGDoutput() returning canvas "' . $this->thumbnail_width . 'x' . $this->thumbnail_height . '"', __FILE__, __LINE__); return true; }
$yi = ImageSY($image); // find the size of the text $box = ImageFTBBox($size, $angle, $font, $text, $extrainfo); $xr = abs(max($box[2], $box[4])); $yr = abs(max($box[5], $box[7])); // compute centering $x = intval(($xi - $xr) / 2); $y = intval(($yi + $yr) / 2); return array($x, $y); } $_GET['text'] = 'I <3 PHP!'; // Configuration settings $image = ImageCreateFromPNG(__DIR__ . '/button.png'); $text = $_GET['text']; $font = '/Library/Fonts/Hei.ttf'; $size = 24; $color = 0x0; $angle = 0; // Print-centered text list($x, $y) = ImageFTCenter($image, $size, $angle, $font, $text); ImageFTText($image, $size, $angle, $x, $y, $color, $font, $text); // Preserve Transparency ImageColorTransparent($image, ImageColorAllocateAlpha($image, 0, 0, 0, 127)); ImageAlphaBlending($image, false); ImageSaveAlpha($image, true); // Send image header('Content-type: image/png'); ImagePNG($image); // Clean up ImagePSFreeFont($font); ImageDestroy($image);
/** * Create a thumbnail image * @param string $fileName The image filename */ function createThumb($fileName, $filePath) { //copy image $oldFile = $this->mediaPath . $filePath . $fileName; $newFile = $this->mediaPath . "thumbs/" . $fileName; $arrSettings = $this->getSettings(); $arrInfo = getimagesize($oldFile); //ermittelt die Größe des Bildes $setSize = $arrSettings['thumbSize']['value']; $strType = $arrInfo[2]; //type des Bildes if ($arrInfo[0] >= $setSize || $arrInfo[1] >= $setSize) { if ($arrInfo[0] <= $arrInfo[1]) { $intFactor = $arrInfo[1] / $setSize; $intHeight = $setSize; $intWidth = $arrInfo[0] / $intFactor; } else { $intFactor = $arrInfo[0] / $setSize; $intResult = $arrInfo[1] / $intFactor; if ($intResult > $setSize) { $intHeight = $setSize; $intWidth = $arrInfo[0] / $intFactor; } else { $intWidth = $setSize; $intHeight = $arrInfo[1] / $intFactor; } } } else { $intWidth = $arrInfo[0]; $intHeight = $arrInfo[1]; } if (imagetypes() & IMG_GIF) { $boolGifEnabled = true; } if (imagetypes() & IMG_JPG) { $boolJpgEnabled = true; } if (imagetypes() & IMG_PNG) { $boolPngEnabled = true; } @touch($newFile); switch ($strType) { case 1: //GIF if ($boolGifEnabled) { $handleImage1 = ImageCreateFromGif($oldFile); $handleImage2 = @ImageCreateTrueColor($intWidth, $intHeight); ImageCopyResampled($handleImage2, $handleImage1, 0, 0, 0, 0, $intWidth, $intHeight, $arrInfo[0], $arrInfo[1]); ImageGif($handleImage2, $newFile); ImageDestroy($handleImage1); ImageDestroy($handleImage2); } break; case 2: //JPG if ($boolJpgEnabled) { $handleImage1 = ImageCreateFromJpeg($oldFile); $handleImage2 = @ImageCreateTrueColor($intWidth, $intHeight); ImageCopyResampled($handleImage2, $handleImage1, 0, 0, 0, 0, $intWidth, $intHeight, $arrInfo[0], $arrInfo[1]); ImageJpeg($handleImage2, $newFile, 95); ImageDestroy($handleImage1); ImageDestroy($handleImage2); } break; case 3: //PNG if ($boolPngEnabled) { $handleImage1 = ImageCreateFromPNG($oldFile); ImageAlphaBlending($handleImage1, true); ImageSaveAlpha($handleImage1, true); $handleImage2 = @ImageCreateTrueColor($intWidth, $intHeight); ImageCopyResampled($handleImage2, $handleImage1, 0, 0, 0, 0, $intWidth, $intHeight, $arrInfo[0], $arrInfo[1]); ImagePNG($handleImage2, $newFile); ImageDestroy($handleImage1); ImageDestroy($handleImage2); } break; } }
} elseif (@$_GET['new']) { // generate a blank image resource of the specified size/background color/opacity if ($phpThumb->w <= 0 || $phpThumb->h <= 0) { $phpThumb->ErrorImage('"w" and "h" parameters required for "new"'); } @(list($bghexcolor, $opacity) = explode('|', $_GET['new'])); if (!phpthumb_functions::IsHexColor($bghexcolor)) { $phpThumb->ErrorImage('BGcolor parameter for "new" is not valid'); } $opacity = strlen($opacity) ? $opacity : 100; if ($phpThumb->gdimg_source = phpthumb_functions::ImageCreateFunction($phpThumb->w, $phpThumb->h)) { $alpha = (100 - min(100, max(0, $opacity))) * 1.27; if ($alpha) { $phpThumb->setParameter('is_alpha', true); ImageAlphaBlending($phpThumb->gdimg_source, false); ImageSaveAlpha($phpThumb->gdimg_source, true); } $new_background_color = phpthumb_functions::ImageHexColorAllocate($phpThumb->gdimg_source, $bghexcolor, false, $alpha); ImageFilledRectangle($phpThumb->gdimg_source, 0, 0, $phpThumb->w, $phpThumb->h, $new_background_color); } else { $phpThumb->ErrorImage('failed to create "new" image (' . $phpThumb->w . 'x' . $phpThumb->h . ')'); } } elseif (!$phpThumb->src) { $phpThumb->ErrorImage('Usage: ' . $_SERVER['PHP_SELF'] . '?src=/path/and/filename.jpg' . "\n" . 'read Usage comments for details'); } elseif (preg_match('/^(f|ht)tp\\:\\/\\//i', $phpThumb->src)) { $phpThumb->DebugMessage('$phpThumb->src (' . $phpThumb->src . ') is remote image, attempting to download', __FILE__, __LINE__); if ($phpThumb->config_http_user_agent) { $phpThumb->DebugMessage('Setting "user_agent" to "' . $phpThumb->config_http_user_agent . '"', __FILE__, __LINE__); ini_set('user_agent', $phpThumb->config_http_user_agent); } $cleanedupurl = phpthumb_functions::CleanUpURLencoding($phpThumb->src);
public function rotate($value = 90, $bgColor = 'transparent') { # Author: Jarrod Oberto # Date: 07-05-2011 # Purpose: Rotate image # Param in: (mixed) $degrees: (int) number of degress to rotate image # (str) param "left": rotate left # (str) param "right": rotate right # (str) param "upside": upside-down image # Param out: # Reference: # Notes: The default direction of imageRotate() is counter clockwise. # if ($this->imageResized) { if (is_integer($value)) { $degrees = $value; } // *** Convert color $rgbArray = $this->formatColor($bgColor); $r = $rgbArray['r']; $g = $rgbArray['g']; $b = $rgbArray['b']; if (isset($rgbArray['a'])) { $a = $rgbArray['a']; } if (is_string($value)) { $value = strtolower($value); switch ($value) { case 'left': $degrees = 90; break; case 'right': $degrees = 270; break; case 'upside': $degrees = 180; break; default: break; } } // *** The default direction of imageRotate() is counter clockwise // * This makes it clockwise $degrees = 360 - $degrees; // *** Create background color $bg = ImageColorAllocateAlpha($this->imageResized, $r, $g, $b, $a); // *** Fill with background ImageFill($this->imageResized, 0, 0, $bg); // *** Rotate $this->imageResized = imagerotate($this->imageResized, $degrees, $bg); // Rotate 45 degrees and allocated the transparent colour as the one to make transparent (obviously) // Ensure alpha transparency ImageSaveAlpha($this->imageResized, true); } }
function ImageResize($image, $paras){ $xcount = ImageSX($image); $ycount = ImageSY($image); if ($paras["width"]){ $newwidth = $paras["width"]; } if ($paras["height"]){ $newheight = $paras["height"]; } if ($newwidth && !$newheight){ $newheight = round($ycount / ($xcount / $newwidth)); } if ($newheight && !$newwidth){ $newwidth = round($xcount / ($ycount / $newheight)); } $newimage = ImageCreateTrueColor($newwidth, $newheight); ImageAlphaBlending($newimage, false); ImageSaveAlpha($newimage, true); ImageCopyResampled($newimage, $image, 0, 0, 0, 0, $newwidth, $newheight, $xcount, $ycount); return $newimage; }
function reduire_image($userfile_name) { global $pmb_vignette_x; global $pmb_vignette_y; global $base_path; global $pmb_curl_available; if (!$pmb_vignette_x) { $pmb_vignette_x = 100; } if (!$pmb_vignette_y) { $pmb_vignette_y = 100; } $src_image = ''; if (file_exists("{$base_path}/temp/{$userfile_name}")) { $bidon = "{$base_path}/temp/{$userfile_name}"; $source_file = $bidon . "[0]"; } else { $bidon = $userfile_name; //Il s'agit d'une url, on copie le fichier en local $nom_temp = session_id() . microtime(); $nom_temp = str_replace(' ', '_', $nom_temp); $nom_temp = str_replace('.', '_', $nom_temp); $fichier_tmp = "{$base_path}/temp/" . $nom_temp; if ($pmb_curl_available) { $aCurl = new Curl(); $aCurl->save_file_name = $fichier_tmp; $aCurl->get($userfile_name); } else { $handle = fopen($userfile_name, "rb"); $filecontent = stream_get_contents($handle); fclose($handle); $fd = fopen($fichier_tmp, "w"); fwrite($fd, $filecontent); fclose($fd); } $source_file = $fichier_tmp . "[0]"; } $error = true; if (extension_loaded('imagick')) { mysql_set_wait_timeout(3600); $error = false; try { $img = new Imagick(); $img->readImage($source_file); if ($img->getImageWidth() > $pmb_vignette_x || $img->getImageHeight() > $pmb_vignette_y) { // Si l'image est trop grande on la réduit $img->thumbnailimage($pmb_vignette_x, $pmb_vignette_y, true); } $img->setImageFormat("png"); $img->setCompression(Imagick::COMPRESSION_LZW); $img->setCompressionQuality(90); $contenu_vignette = $img->getImageBlob(); } catch (Exception $ex) { $error = true; } unlink($fichier_tmp); } if ($error) { $size = @getimagesize($bidon); /* ".gif"=>"1", ".jpg"=>"2", ".jpeg"=>"2", ".png"=>"3", ".swf"=>"4", ".psd"=>"5", ".bmp"=>"6"); */ switch ($size[2]) { case 1: $src_img = imagecreatefromgif($bidon); break; case 2: $src_img = imagecreatefromjpeg($bidon); break; case 3: $src_img = imagecreatefrompng($bidon); break; case 6: $src_img = imagecreatefromwbmp($bidon); break; default: break; } $erreur_vignette = 0; if ($src_img) { $rs = $pmb_vignette_x / $pmb_vignette_y; $taillex = imagesx($src_img); $tailley = imagesy($src_img); if (!$taillex || !$tailley) { return ""; } if ($taillex > $pmb_vignette_x || $tailley > $pmb_vignette_y) { $r = $taillex / $tailley; if ($r < 1 && $rs < 1) { //Si x plus petit que y et taille finale portrait //Si le format final est plus large en proportion if ($rs > $r) { $new_h = $pmb_vignette_y; $new_w = $new_h * $r; } else { $new_w = $pmb_vignette_x; $new_h = $new_w / $r; } } else { if ($r < 1 && $rs >= 1) { //Si x plus petit que y et taille finale paysage $new_h = $pmb_vignette_y; $new_w = $new_h * $r; } else { if ($r > 1 && $rs < 1) { //Si x plus grand que y et taille finale portrait $new_w = $pmb_vignette_x; $new_h = $new_w / $r; } else { //Si x plus grand que y et taille finale paysage if ($rs < $r) { $new_w = $pmb_vignette_x; $new_h = $new_w / $r; } else { $new_h = $pmb_vignette_y; $new_w = $new_h * $r; } } } } } else { $new_h = $tailley; $new_w = $taillex; } $dst_img = imagecreatetruecolor($pmb_vignette_x, $pmb_vignette_y); ImageSaveAlpha($dst_img, true); ImageAlphaBlending($dst_img, false); imagefilledrectangle($dst_img, 0, 0, $pmb_vignette_x, $pmb_vignette_y, imagecolorallocatealpha($dst_img, 0, 0, 0, 127)); imagecopyresized($dst_img, $src_img, round(($pmb_vignette_x - $new_w) / 2), round(($pmb_vignette_y - $new_h) / 2), 0, 0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img)); imagepng($dst_img, "{$base_path}/temp/" . SESSid); $fp = fopen("{$base_path}/temp/" . SESSid, "r"); $contenu_vignette = fread($fp, filesize("{$base_path}/temp/" . SESSid)); if (!$fp || $contenu_vignette == "") { $erreur_vignette++; } fclose($fp); unlink("{$base_path}/temp/" . SESSid); } else { $contenu_vignette = ''; } } return $contenu_vignette; }
function &reduire_image_middle(&$data) { global $opac_photo_mean_size_x; global $opac_photo_mean_size_y; global $opac_photo_watermark; global $opac_photo_watermark_transparency; if ($opac_photo_watermark_transparency == "") { $opac_photo_watermark_transparency = 50; } $src_img = imagecreatefromstring($data); if ($src_img) { $photo_mean_size_x = imagesx($src_img); $photo_mean_size_y = imagesy($src_img); } else { $photo_mean_size_x = 200; $photo_mean_size_y = 200; } if ($opac_photo_mean_size_x) { $photo_mean_size_x = $opac_photo_mean_size_x; } if ($opac_photo_mean_size_y) { $photo_mean_size_y = $opac_photo_mean_size_y; } if ($opac_photo_watermark) { $size = @getimagesize("images/" . $opac_photo_watermark); /* ".gif"=>"1", ".jpg"=>"2", ".jpeg"=>"2", ".png"=>"3", ".swf"=>"4", ".psd"=>"5", ".bmp"=>"6"); */ switch ($size[2]) { case 1: $wat_img = imagecreatefromgif("images/" . $opac_photo_watermark); break; case 2: $wat_img = imagecreatefromjpeg("images/" . $opac_photo_watermark); break; case 3: $wat_img = imagecreatefrompng("images/" . $opac_photo_watermark); break; case 6: $wat_img = imagecreatefromwbmp("images/" . $opac_photo_watermark); break; default: $wat_img = ""; break; } } $erreur_vignette = 0; if ($src_img) { $rs = $photo_mean_size_x / $photo_mean_size_y; $taillex = imagesx($src_img); $tailley = imagesy($src_img); if (!$taillex || !$tailley) { return ""; } if ($taillex > $photo_mean_size_x || $tailley > $photo_mean_size_y) { $r = $taillex / $tailley; if ($r < 1 && $rs < 1) { //Si x plus petit que y et taille finale portrait //Si le format final est plus large en proportion if ($rs > $r) { $new_h = $photo_mean_size_y; $new_w = $new_h * $r; } else { $new_w = $photo_mean_size_x; $new_h = $new_w / $r; } } else { if ($r < 1 && $rs >= 1) { //Si x plus petit que y et taille finale paysage $new_h = $photo_mean_size_y; $new_w = $new_h * $r; } else { if ($r > 1 && $rs < 1) { //Si x plus grand que y et taille finale portrait $new_w = $photo_mean_size_x; $new_h = $new_w / $r; } else { //Si x plus grand que y et taille finale paysage if ($rs < $r) { $new_w = $photo_mean_size_x; $new_h = $new_w / $r; } else { $new_h = $photo_mean_size_y; $new_w = $new_h * $r; } } } } } else { $new_h = $tailley; $new_w = $taillex; } $dst_img = imagecreatetruecolor($photo_mean_size_x, $photo_mean_size_y); ImageSaveAlpha($dst_img, true); ImageAlphaBlending($dst_img, false); imagefilledrectangle($dst_img, 0, 0, $photo_mean_size_x, $photo_mean_size_y, imagecolorallocatealpha($dst_img, 0, 0, 0, 127)); imagecopyresized($dst_img, $src_img, round(($photo_mean_size_x - $new_w) / 2), round(($photo_mean_size_y - $new_h) / 2), 0, 0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img)); if ($wat_img) { $wr_img = imagecreatetruecolor($photo_mean_size_x, $photo_mean_size_y); ImageSaveAlpha($wr_img, true); ImageAlphaBlending($wr_img, false); imagefilledrectangle($wr_img, 0, 0, $photo_mean_size_x, $photo_mean_size_y, imagecolorallocatealpha($dst_img, 0, 0, 0, 127)); imagecopyresized($wr_img, $wat_img, round(($photo_mean_size_x - $new_w) / 2), round(($photo_mean_size_y - $new_h) / 2), 0, 0, $new_w, $new_h, ImageSX($wat_img), ImageSY($wat_img)); imagecopymerge($dst_img, $wr_img, 0, 0, 0, 0, $photo_mean_size_x, $photo_mean_size_y, $opac_photo_watermark_transparency); } imagepng($dst_img, "./temp/" . session_id()); $fp = fopen("./temp/" . session_id(), "r"); $contenu_vignette = fread($fp, filesize("./temp/" . session_id())); if (!$fp || $contenu_vignette == "") { $erreur_vignette++; } fclose($fp); unlink("./temp/" . session_id()); } else { $contenu_vignette = ""; } return $contenu_vignette; }
function Image($image, $crop = null, $size = null, $user) { $image = ImageCreateFromString(file_get_contents($image)); if (is_resource($image) === true) { $x = 0; $y = 0; $width = imagesx($image); $height = imagesy($image); /* CROP (Aspect Ratio) Section */ if (is_null($crop) === true) { $crop = array($width, $height); } else { $crop = array_filter(explode(':', $crop)); if (empty($crop) === true) { $crop = array($width, $height); } else { if (empty($crop[0]) === true || is_numeric($crop[0]) === false) { $crop[0] = $crop[1]; } else { if (empty($crop[1]) === true || is_numeric($crop[1]) === false) { $crop[1] = $crop[0]; } } } $ratio = array(0 => $width / $height, 1 => $crop[0] / $crop[1]); if ($ratio[0] > $ratio[1]) { $width = $height * $ratio[1]; $x = (imagesx($image) - $width) / 2; } else { if ($ratio[0] < $ratio[1]) { $height = $width / $ratio[1]; $y = (imagesy($image) - $height) / 2; } } } /* Resize Section */ if (is_null($size) === true) { $size = array($width, $height); } else { $size = array_filter(explode('x', $size)); if (empty($size) === true) { $size = array(imagesx($image), imagesy($image)); } else { if (empty($size[0]) === true || is_numeric($size[0]) === false) { $size[0] = round($size[1] * $width / $height); } else { if (empty($size[1]) === true || is_numeric($size[1]) === false) { $size[1] = round($size[0] * $height / $width); } } } } $result = ImageCreateTrueColor($size[0], $size[1]); if (is_resource($result) === true) { ImageSaveAlpha($result, true); ImageAlphaBlending($result, true); ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255)); ImageCopyResampled($result, $image, 0, 0, $x, $y, $size[0], $size[1], $width, $height); ImageInterlace($result, true); $file1 = $result; $file2 = 'sim33.png'; // Second image (the overlay) $overlay = imagecreatefrompng($file2); // We need to know the width and height of the overlay list($widthx, $heightx, $type, $attr) = getimagesize($file2); // Apply the overlay imagecopy($file1, $overlay, 0, 0, 0, 0, $widthx, $heightx); imagedestroy($overlay); // Output the results imagejpeg($file1, 'pics/avatar-' . $user . '.jpg', 90); echo '<img src="pics/avatar-' . $user . '.jpg" alt="">'; imagedestroy($file1); //ImageJPEG($result, null, 90); } } return false; }
function resizeToDisplay() { $src_img = imagecreatefromstring($this->driver->openCurrentDoc()); if ($src_img) { $photo_mean_size_x = imagesx($src_img); $photo_mean_size_y = imagesy($src_img); } else { $photo_mean_size_x = 200; $photo_mean_size_y = 200; } $maxX = $this->parameters["size_x"]; $maxY = $this->parameters["size_y"]; if ($maxX) { $photo_mean_size_x = $maxX; } if ($maxY) { $photo_mean_size_y = $maxY; } if ($src_img) { $rs = $photo_mean_size_x / $photo_mean_size_y; $taillex = imagesx($src_img); $tailley = imagesy($src_img); if (!$taillex || !$tailley) { return ""; } if ($taillex > $photo_mean_size_x || $tailley > $photo_mean_size_y) { $r = $taillex / $tailley; if ($r < 1 && $rs < 1) { //Si x plus petit que y et taille finale portrait //Si le format final est plus large en proportion if ($rs > $r) { $new_h = $photo_mean_size_y; $new_w = $new_h * $r; } else { $new_w = $photo_mean_size_x; $new_h = $new_w / $r; } } else { if ($r < 1 && $rs >= 1) { //Si x plus petit que y et taille finale paysage $new_h = $photo_mean_size_y; $new_w = $new_h * $r; } else { if ($r > 1 && $rs < 1) { //Si x plus grand que y et taille finale portrait $new_w = $photo_mean_size_x; $new_h = $new_w / $r; } else { //Si x plus grand que y et taille finale paysage if ($rs < $r) { $new_w = $photo_mean_size_x; $new_h = $new_w / $r; } else { $new_h = $photo_mean_size_y; $new_w = $new_h * $r; } } } } } else { $new_h = $tailley; $new_w = $taillex; } $dst_img = imagecreatetruecolor($new_w, $new_h); ImageSaveAlpha($dst_img, true); ImageAlphaBlending($dst_img, false); imagefilledrectangle($dst_img, 0, 0, $photo_mean_size_x, $photo_mean_size_y, imagecolorallocatealpha($dst_img, 0, 0, 0, 127)); imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img)); $watermark = $this->driver->getUrlImage($this->parameters['watermark']); if ($watermark != "") { $size = @getimagesize($watermark); switch ($size[2]) { case 1: $wat_img = imagecreatefromgif($watermark); break; case 2: $wat_img = imagecreatefromjpeg($watermark); break; case 3: $wat_img = imagecreatefrompng($watermark); break; case 6: $wat_img = imagecreatefromwbmp($watermark); break; default: $wat_img = ""; break; } if ($wat_img) { $wr_img = imagecreatetruecolor($new_w, $new_h); imagecolortransparent($wr_img, imagecolorallocatealpha($wr_img, 0, 0, 0, 127)); ImageSaveAlpha($wr_img, true); ImageAlphaBlending($wr_img, false); imagefilledrectangle($wr_img, 0, 0, $new_w, $new_h, imagecolorallocatealpha($wr_img, 0, 0, 0, 127)); imagecopyresized($wr_img, $wat_img, 0, 0, 0, 0, $new_w, $new_h, ImageSX($wat_img), ImageSY($wat_img)); imagecopymerge($dst_img, $wr_img, 0, 0, 0, 0, $new_w, $new_h, $this->parameters['transparence']); } } imagepng($dst_img); } }
function ApplyMask(&$gdimg_mask, &$gdimg_image) { if (gd_version() < 2) { $DebugMessage = 'Skipping ApplyMask() because gd_version is "' . gd_version() . '"' . __FILE__ . __LINE__; return false; } if (version_compare_replacement(phpversion(), '4.3.2', '>=')) { $DebugMessage = 'Using alpha ApplyMask() technique' . __FILE__ . __LINE__; if ($gdimg_mask_resized = ImageCreateTrueColor(ImageSX($gdimg_image), ImageSY($gdimg_image))) { ImageCopyResampled($gdimg_mask_resized, $gdimg_mask, 0, 0, 0, 0, ImageSX($gdimg_image), ImageSY($gdimg_image), ImageSX($gdimg_mask), ImageSY($gdimg_mask)); if ($gdimg_mask_blendtemp = ImageCreateTrueColor(ImageSX($gdimg_image), ImageSY($gdimg_image))) { $color_background = ImageColorAllocate($gdimg_mask_blendtemp, 0, 0, 0); ImageFilledRectangle($gdimg_mask_blendtemp, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp), $color_background); ImageAlphaBlending($gdimg_mask_blendtemp, false); ImageSaveAlpha($gdimg_mask_blendtemp, true); for ($x = 0; $x < ImageSX($gdimg_image); $x++) { for ($y = 0; $y < ImageSY($gdimg_image); $y++) { //$RealPixel = phpthumb_functions::GetPixelColor($gdimg_mask_blendtemp, $x, $y); $RealPixel = GetPixelColor($gdimg_image, $x, $y); $MaskPixel = GrayscalePixel(GetPixelColor($gdimg_mask_resized, $x, $y)); $MaskAlpha = 127 - floor($MaskPixel['red'] / 2) * (1 - $RealPixel['alpha'] / 127); $newcolor = ImageColorAllocateAlphaSafe($gdimg_mask_blendtemp, $RealPixel['red'], $RealPixel['green'], $RealPixel['blue'], $MaskAlpha); ImageSetPixel($gdimg_mask_blendtemp, $x, $y, $newcolor); } } ImageAlphaBlending($gdimg_image, false); ImageSaveAlpha($gdimg_image, true); ImageCopy($gdimg_image, $gdimg_mask_blendtemp, 0, 0, 0, 0, ImageSX($gdimg_mask_blendtemp), ImageSY($gdimg_mask_blendtemp)); ImageDestroy($gdimg_mask_blendtemp); } else { $DebugMessage = 'ImageCreateFunction() failed' . __FILE__ . __LINE__; } ImageDestroy($gdimg_mask_resized); } else { $DebugMessage = 'ImageCreateFunction() failed' . __FILE__ . __LINE__; } } else { // alpha merging requires PHP v4.3.2+ $DebugMessage = 'Skipping ApplyMask() technique because PHP is v"' . phpversion() . '"' . __FILE__ . __LINE__; } return true; }
function Resize_Image($src, $width = 0, $height = 0, $dst) { if ($height <= 0 && $width <= 0) { return false; } // Setting defaults and meta $image = ''; $final_width = 0; $final_height = 0; list($width_old, $height_old, $image_type) = GetImageSize($src); // Calculating proportionality if ($width == 0) { $factor = $height / $height_old; } elseif ($height == 0) { $factor = $width / $width_old; } else { $factor = Min($width / $width_old, $height / $height_old); } $final_width = Round($width_old * $factor); $final_height = Round($height_old * $factor); // Loading image to memory according to type switch ($image_type) { case IMAGETYPE_GIF: $image = imagecreatefromgif($src); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($src); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($src); break; default: return false; } // This is the resizing/resampling/transparency-preserving magic $image_resized = ImageCreateTrueColor($final_width, $final_height); if ($image_type == IMAGETYPE_GIF || $image_type == IMAGETYPE_PNG) { $transparency = ImageColorTransparent($image); if ($image_type == IMAGETYPE_GIF && $transparency >= 0) { list($r, $g, $b) = Array_Values(ImageColorsForIndex($image, $transparency)); $transparency = ImageColorAllocate($image_resized, $r, $g, $b); Imagefill($image_resized, 0, 0, $transparency); ImageColorTransparent($image_resized, $transparency); } elseif ($image_type == IMAGETYPE_PNG) { ImageAlphaBlending($image_resized, false); $color = ImageColorAllocateAlpha($image_resized, 0, 0, 0, 127); ImageFill($image_resized, 0, 0, $color); ImageSaveAlpha($image_resized, true); } } ImageCopyResampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); // Writing image switch ($image_type) { case IMAGETYPE_GIF: imagegif($image_resized, $dst); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $dst, 85); break; case IMAGETYPE_PNG: imagepng($image_resized, $dst); break; default: return false; } }
/** * 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); }
static function load($filename, $format = NULL) { $image = new Image(); //检查文件是否存在 if (!file_exists($filename)) { throw new Error_Exception(T('文件%filename无法找到.', array('%filename' => $filename))); } elseif (!is_readable($filename)) { throw new Error_Exception(T('文件%filename无法读取.', array('%filename' => $filename))); } if (!$format) { $format = File::extension($filename); } switch (strtolower($format)) { case 'gif': $image->format = 'gif'; break; case 'jpg': case 'jpeg': $image->format = 'jpg'; break; case 'png': $image->format = 'png'; break; default: throw new Error_Exception(T('文件%filename不是可识别的图片格式(GIF/JPG/PNG)', array('%filename' => $filename))); } $image->filename = $filename; //init resources if no errors switch ($image->format) { case 'gif': $image->im = @ImageCreateFromGif($image->filename); break; case 'jpg': $image->im = @ImageCreateFromJpeg($image->filename); break; case 'png': $image->im = @ImageCreateFromPng($image->filename); break; } if (!$image->im) { throw new Error_Exception(T('文件%filename不是可识别的图片格式(GIF/JPG/PNG)', array('%filename' => $filename))); } $size = GetImageSize($image->filename); list($image->curr_width, $image->curr_height) = $size; if ($image->format == 'png') { $im = ImageCreateTrueColor($size[0], $size[1]); ImageAlphaBlending($im, false); ImageSaveAlpha($im, true); ImageCopy($im, $image->im, 0, 0, 0, 0, $size[0], $size[1]); $image->im = $im; } return $image; }
<?php // Rectangle Version $filename = __DIR__ . '/php.png'; // Thumbnail Dimentions $w = 50; $h = 20; // Images $original = ImageCreateFromPNG($filename); $thumbnail = ImageCreateTrueColor($w, $h); // Preserve Transparency ImageColorTransparent($thumbnail, ImageColorAllocateAlpha($thumbnail, 0, 0, 0, 127)); ImageAlphaBlending($thumbnail, false); ImageSaveAlpha($thumbnail, true); // Scale & Copy $x = ImageSX($original); $y = ImageSY($original); $scale = min($x / $w, $y / $h); ImageCopyResampled($thumbnail, $original, 0, 0, ($x - $w * $scale) / 2, ($y - $h * $scale) / 2, $w, $h, $w * $scale, $h * $scale); // Send header('Content-type: image/png'); ImagePNG($thumbnail); ImageDestroy($original); ImageDestroy($thumbnail);
protected function resize($size_x = 0, $size_y = 0) { if ($this->data) { $src_img = imagecreatefromstring($this->data); $maxX = $size_x; $maxY = $size_y; if (!$size_x && !$size_y) { ImageSaveAlpha($src_img, true); ImageAlphaBlending($src_img, false); imagepng($src_img); } else { if ($src_img) { $rs = $maxX / $maxY; $taillex = imagesx($src_img); $tailley = imagesy($src_img); if (!$taillex || !$tailley) { return ""; } if ($taillex > $maxX || $tailley > $maxY) { $r = $taillex / $tailley; if ($r < 1 && $rs < 1) { //Si x plus petit que y et taille finale portrait //Si le format final est plus large en proportion if ($rs > $r) { $new_h = $maxY; $new_w = $new_h * $r; } else { $new_w = $maxX; $new_h = $new_w / $r; } } else { if ($r < 1 && $rs >= 1) { //Si x plus petit que y et taille finale paysage $new_h = $maxY; $new_w = $new_h * $r; } else { if ($r > 1 && $rs < 1) { //Si x plus grand que y et taille finale portrait $new_w = $maxX; $new_h = $new_w / $r; } else { //Si x plus grand que y et taille finale paysage if ($rs < $r) { $new_w = $maxX; $new_h = $new_w / $r; } else { $new_h = $maxY; $new_w = $new_h * $r; } } } } } else { $new_h = $tailley; $new_w = $taillex; } $dst_img = imagecreatetruecolor($new_w, $new_h); ImageSaveAlpha($dst_img, true); ImageAlphaBlending($dst_img, false); imagefilledrectangle($dst_img, 0, 0, $maxX, $maxY, imagecolorallocatealpha($dst_img, 0, 0, 0, 127)); imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, ImageSX($src_img), ImageSY($src_img)); imagepng($dst_img); } } } else { print file_get_contents("./images/vide.png"); } }
public static function beResizeImg($isrc, $idst, $iw, $ih, $ip = 'keep', $imax = 'yes', $iqual = 95) { /* parameters $isrc source file $idst destination folder $iw new width $ih new height $ip='keep' keep proportions $imax='yes' treat width/height as maximums $iqual=95 image quality */ //read array with source image information $imagedata = getimagesize($isrc); //calculate new width and height $srcwid = $imagedata[0]; $srchei = $imagedata[1]; $srcx = $srcy = 0; //if no height is set we calculate it if ($ih == "full") { $ih = (int) ($imagedata[1] * ($iw / $imagedata[0])); } //the new width $new_iw = (int) $iw; //the new height if ($ip == 'keep') { //keep proportions $new_ih = (int) ($imagedata[1] * ($new_iw / $imagedata[0])); if ($new_ih > $ih) { $new_ih = (int) $ih; $new_iw = (int) ($imagedata[0] * ($new_ih / $imagedata[1])); } } else { //set fixed height $new_ih = (int) $ih; //crop if ($imagedata[0] / $new_iw < $imagedata[1] / $new_ih) { //original to heigh $srchei = (int) ($new_ih * ($imagedata[0] / $new_iw)); $srcy = (int) (($imagedata[1] - $srchei) / 2); } elseif ($imagedata[0] / $new_iw > $imagedata[1] / $new_ih) { //original to wide $srcwid = (int) ($new_iw * ($imagedata[1] / $new_ih)); $srcx = (int) (($imagedata[0] - $srcwid) / 2); } } //set image type and set image name $ipath = pathinfo($isrc); $itype = strtolower($ipath["extension"]); $iname = substr($ipath["basename"], 0, -(strlen($itype) + 1)) . "_" . $new_iw . "_" . $new_ih . "_" . $iqual . "." . $itype; //check if $idst is set to a subdirectory and if so add it to the path $idir = $idst != "" ? $ipath["dirname"] . "/" . $idst : $ipath["dirname"]; //check if image exists, else create it if (!JFile::exists($idir . "/" . $iname)) { if ($itype == "jpg" && function_exists("imagecreatefromjpeg")) { $image = imagecreatefromjpeg($isrc); $image_dest = imagecreatetruecolor($new_iw, $new_ih); imagecopyresampled($image_dest, $image, 0, 0, $srcx, $srcy, $new_iw, $new_ih, $srcwid, $srchei); ob_start(); // start a new output buffer imagejpeg($image_dest, NULL, $iqual); $buffer = ob_get_contents(); ob_end_clean(); // stop this output buffer } elseif ($itype == "gif" && function_exists("imagecreatefromgif")) { $image = imagecreatefromgif($isrc); $image_dest = imagecreatetruecolor($new_iw, $new_ih); imagealphablending($image_dest, false); // get and reallocate transparency-color $transindex = imagecolortransparent($image); if ($transindex >= 0) { $transcol = imagecolorsforindex($image, $transindex); $transindex = imagecolorallocatealpha($image_dest, $transcol['red'], $transcol['green'], $transcol['blue'], 127); imagefill($image_dest, 0, 0, $transindex); } // resample imagecopyresampled($image_dest, $image, 0, 0, $srcx, $srcy, $new_iw, $new_ih, $srcwid, $srchei); // restore transparency if ($transindex >= 0) { imagecolortransparent($image_dest, $transindex); for ($y = 0; $y < $new_ih; ++$y) { for ($x = 0; $x < $new_iw; ++$x) { if ((imagecolorat($image_dest, $x, $y) >> 24 & 0x7f) >= 100) { imagesetpixel($image_dest, $x, $y, $transindex); } } } } imagetruecolortopalette($image_dest, true, 255); imagesavealpha($image_dest, false); ob_start(); // start a new output buffer imagegif($image_dest, NULL, $iqual); $buffer = ob_get_contents(); ob_end_clean(); // stop this output buffer } elseif ($itype == "png" && function_exists("imagecreatefrompng")) { $image = ImageCreateFromPng($isrc); $image_dest = ImageCreateTrueColor($new_iw, $new_ih); $transindex = imagecolortransparent($image); $istruecolor = imageistruecolor($image); if ($transindex >= 0) { ImageColorTransparent($image_dest, ImageColorAllocate($image_dest, 0, 0, 0)); ImageAlphaBlending($image_dest, false); } elseif (!$istruecolor) { ImagePaletteCopy($image_dest, $image); } else { ImageColorTransparent($image_dest, ImageColorAllocate($image_dest, 0, 0, 0)); ImageAlphaBlending($image_dest, false); ImageSaveAlpha($image_dest, true); } ImageCopyResized($image_dest, $image, 0, 0, $srcx, $srcy, $new_iw, $new_ih, $srcwid, $srchei); $iqual_png = 100 - $iqual; if (substr(phpversion(), 0, 1) >= 5) { $iqual_png = intval(($iqual - 10) / 10); } ob_start(); // start a new output buffer Imagepng($image_dest, NULL, $iqual_png); $buffer = ob_get_contents(); ob_end_clean(); // stop this output buffer } if (isset($buffer) && $buffer != "") { JFile::write($idir . '/' . $iname, $buffer); unset($buffer); } if (isset($image)) { imagedestroy($image); } if (isset($image_dest)) { imagedestroy($image_dest); } } //utf8_encode and rawurlencode file name $iname = rawurlencode(utf8_encode($iname)); //return path/filename/type/width/height return $thenewimage = array($idir . '/' . $iname, $iname, $itype, $new_iw, $new_ih); }
static function createWatermarkImage($source_dir, $destination_dir, $file_name, $delete_existing_file = false, $watermark_text = '', $args = array()) { global $wp_photo_gallery; if ($watermark_text === '') { $watermark_text = ' '; //get_site_url(); } $watermark_image_name = 'watermark_' . $file_name; $dest = $destination_dir . $watermark_image_name; if (file_exists($dest)) { if ($delete_existing_file === false) { return; } } $sourch_file = $source_dir . $file_name; $image_info = getimagesize($sourch_file); list($width, $height) = $image_info; $mime_type = strtolower($image_info['mime']); $desired_height = isset($args['watermark_height']) ? $args['watermark_height'] : ''; $desired_width = isset($args['watermark_width']) ? $args['watermark_width'] : ''; $font_size = isset($args['watermark_font_size']) ? $args['watermark_font_size'] : ''; $watermark_placement = isset($args['watermark_placement']) ? $args['watermark_placement'] : '0'; $watermark_opacity = isset($args['watermark_opacity']) ? $args['watermark_opacity'] : '35'; //$watermark_colour = isset($args['watermark_colour'])?$args['watermark_colour']:'ffffff'; //TODO - introduce in settings using a colour picker if ($desired_height != '') { //we have a portrait image so use the height as the maximum dimension if ($desired_height > $height) { $desired_height = $height; } //Check to make sure the watermarked image is not larger than the original $desired_width = floor($width * ($desired_height / $height)); } else { //we have a landscape image so use the width as the maximum dimension if (empty($desired_width)) { $desired_width = 600; } if ($desired_width > $width) { $desired_width = $width; } //Check to make sure the watermarked image is not larger than the original $desired_height = floor($height * ($desired_width / $width)); } if (empty($font_size)) { $font_size = 35; } if ($mime_type == 'image/jpeg' || $mime_type == 'image/pjpeg') { $image = imagecreatefromjpeg($sourch_file); } else { if ($mime_type == 'image/png') { $image = imagecreatefrompng($sourch_file); } } $font = WP_PHOTO_PATH . '/fonts' . DIRECTORY_SEPARATOR . 'arial.ttf'; $TextSize = ImageTTFBBox($font_size, 0, $font, $watermark_text) or die; $TextWidth = abs($TextSize[2]) + abs($TextSize[0]) + 8; //Added an extra 8 pixels because otherwise the watermark text appeared slightly cut-off on the RHS $TextHeight = abs($TextSize[7]) + abs($TextSize[1]); // Create Image for Text $image_p = ImageCreateTrueColor($TextWidth, $TextHeight); ImageSaveAlpha($image_p, true); ImageAlphaBlending($image_p, false); $bgText = imagecolorallocatealpha($image_p, 255, 255, 255, 127); imagefill($image_p, 0, 0, $bgText); $watermark_transparency = 127 - $watermark_opacity * 1.27; $color = 'ffffff'; //TODO - introduce in settings using a colour picker $rgb = WPPGPhotoGallery::hex2rgb($color, false); $TextColor = imagecolorallocatealpha($image_p, $rgb[0], $rgb[1], $rgb[2], $watermark_transparency); // Create Text on image imagettftext($image_p, $font_size, 0, 0, abs($TextSize[5]), $TextColor, $font, $watermark_text); $watermark_img_path = $image_p; imagealphablending($image_p, false); imagesavealpha($image_p, true); $sourcefile_width = imageSX($image); $sourcefile_height = imageSY($image); $watermarkfile_width = imageSX($image_p); $watermarkfile_height = imageSY($image_p); if ($watermark_placement == '0') { //Centre $dest_x = $sourcefile_width / 2 - $watermarkfile_width / 2; $dest_y = $sourcefile_height / 2 - $watermarkfile_height / 2; } elseif ($watermark_placement == '1') { //Top Left $dest_x = 5; $dest_y = 5; } elseif ($watermark_placement == '2') { //Top Right $dest_x = $sourcefile_width - $watermarkfile_width - 5; $dest_y = 5; } elseif ($watermark_placement == '3') { //Bottom Right $dest_x = $sourcefile_width - $watermarkfile_width - 5; $dest_y = $sourcefile_height - $watermarkfile_height - 5; } elseif ($watermark_placement == '4') { //Bottom Left $dest_x = 5; $dest_y = $sourcefile_height - $watermarkfile_height - 5; } else { //default - Centre $dest_x = $sourcefile_width / 2 - $watermarkfile_width / 2; $dest_y = $sourcefile_height / 2 - $watermarkfile_height / 2; } if ($watermark_placement != '5') { //Place the image according to the co-ordinates calculated from above imagecopy($image, $image_p, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height); } else { //Display watermark text as repeated grid $top = 20; while ($top < $sourcefile_height) { $left = 10; while ($left < $sourcefile_width) { imagecopy($image, $image_p, $left, $top, 0, 0, $TextWidth, $TextHeight); $left = $left + $TextWidth + 50; } $top = $top + $TextHeight + 50; } } /* create the physical watermarked image to its destination */ imagejpeg($image, $dest, 100); imagedestroy($image); //clean up some memory $resized = image_make_intermediate_size($source_dir . $watermark_image_name, $desired_width, $desired_height); //Use the WP function to resize the watermarked image to that specified in the settings if ($resized === false) { $wp_photo_gallery->debug_logger->log_debug('WPPGPhotoGallery::createWatermarkImage - image_make_intermediate_size failed and returned false!', 4); } else { rename($source_dir . $resized['file'], $source_dir . $watermark_image_name); //Since the above WP function uses a different naming convention we will change the name back to our convention } }
public function rotate($value = 90, $bgColor = 'transparent') { if ($this->imageResized) { if (is_integer($value)) { $degrees = $value; } // *** Convert color $rgbArray = $this->formatColor($bgColor); $r = $rgbArray['r']; $g = $rgbArray['g']; $b = $rgbArray['b']; if (isset($rgbArray['a'])) { $a = $rgbArray['a']; } if (is_string($value)) { $value = fix_strtolower($value); switch ($value) { case 'left': $degrees = 90; break; case 'right': $degrees = 270; break; case 'upside': $degrees = 180; break; default: break; } } // *** The default direction of imageRotate() is counter clockwise // * This makes it clockwise $degrees = 360 - $degrees; // *** Create background color $bg = ImageColorAllocateAlpha($this->imageResized, $r, $g, $b, $a); // *** Fill with background ImageFill($this->imageResized, 0, 0, $bg); // *** Rotate $this->imageResized = imagerotate($this->imageResized, $degrees, $bg); // Rotate 45 degrees and allocated the transparent colour as the one to make transparent (obviously) // Ensure alpha transparency ImageSaveAlpha($this->imageResized, true); } }
function ImageCR($source, $crop = null, $scale = null, $destination = null, $extension = null) { $source = @ImageCreateFromString(@file_get_contents($source)); if (is_resource($source) === true) { $size = array(ImageSX($source), ImageSY($source)); //check to see if the source image is the same size as required destination if (isset($scale) === true) { $scale = array_filter(explode('*', $scale), 'is_numeric'); //echo var_dump($size); if ($scale[0] == $size[0] && $scale[1] == $size[1] && $extension != "gif") { //then simply return we don't need to modify the file.. /*$content = PHP_EOL.'============================================'.PHP_EOL; $content .= strftime('%c').PHP_EOL; $content .= print_r($tscale, true).PHP_EOL.PHP_EOL; $content .= print_r($size, true).PHP_EOL; $content .= '============================================'.PHP_EOL.PHP_EOL; $fp = fopen('file.txt', 'a'); fwrite($fp, $content); fclose($fp);*/ return true; } } if (isset($crop) === true) { $crop = array_filter(explode('/', $crop), 'is_numeric'); if (count($crop) == 2) { $crop = array($size[0] / $size[1], $crop[0] / $crop[1]); if ($crop[0] > $crop[1]) { $size[0] = $size[1] * $crop[1]; } else { if ($crop[0] < $crop[1]) { $size[1] = $size[0] / $crop[1]; } } $crop = array(ImageSX($source) - $size[0], ImageSY($source) - $size[1]); } else { $crop = array(0, 0); } } else { $crop = array(0, 0); } if (isset($scale) === true) { //$scale = array_filter(explode('*', $scale), 'is_numeric'); if (count($scale) >= 1) { if (empty($scale[0]) === true) { $scale[0] = $scale[1] * $size[0] / $size[1]; } else { if (empty($scale[1]) === true) { $scale[1] = $scale[0] * $size[1] / $size[0]; } } } else { $scale = array($size[0], $size[1]); } } else { $scale = array($size[0], $size[1]); } $result = ImageCreateTrueColor($scale[0], $scale[1]); if (is_resource($result) === true) { ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT); ImageSaveAlpha($result, true); ImageAlphaBlending($result, true); if (ImageCopyResampled($result, $source, 0, 0, $crop[0] / 2, $crop[1] / 2, $scale[0], $scale[1], $size[0], $size[1]) === true) { //origpath $origpath = $destination . "." . $extension; if (preg_match('~gif$~i', $origpath) >= 1) { //return ImageGIF($result, $destination.".".$extension); //imagecreatetruecolor /*if(file_exists($origpath)) { unlink($origpath); } if(ImageJPEG($result, $destination.".jpg", 90)) { return "jpg"; } else { return false; } */ if (ImageGIF($result, $origpath, 9)) { return "gif"; } else { return false; } //return ImageJPEG($result, $destination.".".$extension, 90); } else { if (preg_match('~png$~i', $origpath) >= 1) { if (ImagePNG($result, $origpath, 9)) { return "png"; } else { return false; } } else { if (preg_match('~jpe?g$~i', $origpath) >= 1) { if (ImageJPEG($result, $origpath, 90)) { return "jpg"; } else { return false; } } } } } } } return false; }
public function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment = '*', $opacity = 50, $margin_x = 5, $margin_y = null) { if (is_resource($gdimg_dest) && is_resource($img_watermark)) { $watermark_source_x = 0; $watermark_source_y = 0; $img_source_width = ImageSX($gdimg_dest); $img_source_height = ImageSY($gdimg_dest); $watermark_source_width = ImageSX($img_watermark); $watermark_source_height = ImageSY($img_watermark); $watermark_opacity_percent = max(0, min(100, $opacity)); $margin_y = is_null($margin_y) ? $margin_x : $margin_y; $watermark_margin_x = $margin_x > 0 && $margin_x < 1 ? round((1 - $margin_x) * $img_source_width) : $margin_x; $watermark_margin_y = $margin_y > 0 && $margin_y < 1 ? round((1 - $margin_y) * $img_source_height) : $margin_y; if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) { $watermark_destination_x = intval($matches[1]); $watermark_destination_y = intval($matches[2]); } else { switch ($alignment) { case '*': if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) { ImageAlphaBlending($gdimg_tiledwatermark, false); ImageSaveAlpha($gdimg_tiledwatermark, true); $text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127); ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent); // set the tiled image transparent color to whatever the untiled image transparency index is // ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark)); // a "cleaner" way of doing it, but can't handle the margin feature :( // ImageSetTile($gdimg_tiledwatermark, $img_watermark); // ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED); // break; // ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark)); // tile the image as many times as can fit for ($x = $watermark_margin_x; $x < $img_source_width + $watermark_source_width; $x += $watermark_source_width + $watermark_margin_x) { for ($y = $watermark_margin_y; $y < $img_source_height + $watermark_source_height; $y += $watermark_source_height + $watermark_margin_y) { ImageCopy($gdimg_tiledwatermark, $img_watermark, $x, $y, 0, 0, min($watermark_source_width, $img_source_width - $x - $watermark_margin_x), min($watermark_source_height, $img_source_height - $y - $watermark_margin_y)); } } $watermark_source_width = ImageSX($gdimg_tiledwatermark); $watermark_source_height = ImageSY($gdimg_tiledwatermark); $watermark_destination_x = 0; $watermark_destination_y = 0; ImageDestroy($img_watermark); $img_watermark = $gdimg_tiledwatermark; } break; case 'T': $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2 + $watermark_margin_x); $watermark_destination_y = $watermark_margin_y; break; case 'B': $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2 + $watermark_margin_x); $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y; break; case 'L': $watermark_destination_x = $watermark_margin_x; $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2 + $watermark_margin_y); break; case 'R': $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x; $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2 + $watermark_margin_y); break; case 'C': $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2); $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2); break; case 'TL': $watermark_destination_x = $watermark_margin_x; $watermark_destination_y = $watermark_margin_y; break; case 'TR': $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x; $watermark_destination_y = $watermark_margin_y; break; case 'BL': //echo '<pre>'; ////var_dump($watermark_destination_x); ////var_dump($watermark_destination_y); //var_dump($watermark_margin_x); //var_dump($img_source_height); //var_dump($watermark_source_height); //var_dump($watermark_margin_y); $watermark_destination_x = $watermark_margin_x; $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y; break; case 'BR': default: $watermark_destination_x = $img_source_width - $watermark_source_width - $watermark_margin_x; $watermark_destination_y = $img_source_height - $watermark_source_height - $watermark_margin_y; break; } } ImageAlphaBlending($gdimg_dest, false); ImageSaveAlpha($gdimg_dest, true); ImageSaveAlpha($img_watermark, true); phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent); return true; } return false; }
function create_watermark($file) { //文件不存在则返回 if (!file_exists($this->watermark_file) || !file_exists($file)) { return; } if (!function_exists('getImageSize')) { return; } //检查GD支持的文件类型 $gd_allow_types = array(); if (function_exists('ImageCreateFromGIF')) { $gd_allow_types['image/gif'] = 'ImageCreateFromGIF'; } if (function_exists('ImageCreateFromPNG')) { $gd_allow_types['image/png'] = 'ImageCreateFromPNG'; } if (function_exists('ImageCreateFromJPEG')) { $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG'; } //获取文件信息 $fileinfo = getImageSize($file); $wminfo = getImageSize($this->watermark_file); if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) { return; } if (array_key_exists($fileinfo['mime'], $gd_allow_types)) { if (array_key_exists($wminfo['mime'], $gd_allow_types)) { // 从文件创建图像 $temp = $gd_allow_types[$fileinfo['mime']]($file); $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file); // 水印位置 switch ($this->watermark_pos) { case 1: //顶部居左 $dst_x = 0; $dst_y = 0; break; case 2: //顶部居中 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break; case 3: //顶部居右 $dst_x = $fileinfo[0]; $dst_y = 0; break; case 4: //底部居左 $dst_x = 0; $dst_y = $fileinfo[1]; break; case 5: //底部居中 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break; case 6: //底部居右 $dst_x = $fileinfo[0] - $wminfo[0]; $dst_y = $fileinfo[1] - $wminfo[1]; break; default: //随机 $dst_x = mt_rand(0, $fileinfo[0] - $wminfo[0]); $dst_y = mt_rand(0, $fileinfo[1] - $wminfo[1]); } if (function_exists('ImageAlphaBlending')) { ImageAlphaBlending($temp_wm, True); } // 设定图像的混色模式 if (function_exists('ImageSaveAlpha')) { ImageSaveAlpha($temp_wm, True); } // 保存完整的 alpha 通道信息 //为图像添加水印 if (function_exists('imageCopyMerge')) { ImageCopyMerge($temp, $temp_wm, $dst_x, $dst_y, 0, 0, $wminfo[0], $wminfo[1], $this->watermark_trans); } else { ImageCopyMerge($temp, $temp_wm, $dst_x, $dst_y, 0, 0, $wminfo[0], $wminfo[1]); } // 保存图片 switch ($fileinfo['mime']) { case 'image/jpeg': @imageJPEG($temp, $file); break; case 'image/png': @imagePNG($temp, $file); break; case 'image/gif': @imageGIF($temp, $file); break; } // 销毁零时图像 @imageDestroy($temp); @imageDestroy($temp_wm); } } }
} $color = null; $inImg = null; switch (strtoupper($srcPathInfo["extension"])) { case "JPEG": case "JPG": $inImg = ImageCreateFromJPEG($rootPath ."/". $arResult["SOURCE_IMAGE"]["SRC"]); if ($hasColor) { $color = ImageColorAllocate($outImg, $R, $G, $B); } break; case "PNG": $inImg = ImageCreateFromPNG($rootPath ."/". $arResult["SOURCE_IMAGE"]["SRC"]); ImageAlphaBlending($outImg, false); ImageSaveAlpha($outImg, true); if ($hasColor) { if ($arParams["JPEG_OUTPUT"] == "CONV_PNG_DIFF_Q" || $arParams["JPEG_OUTPUT"] == "CONV_PNG_SET_Q") { $color = ImageColorAllocate($outImg, $R, $G, $B); } else { $color = ImageColorAllocateAlpha($outImg, $R, $G, $B, $A); } } break; } if( $hasColor && ($arParams["FILL_ALWAYS"] == "Y" || ($arParams["RESIZE_TYPE"] == "CROP" && $arParams["KEEP_SIZE"] == "FILL") ) ) { ImageFill($outImg, 0, 0, $color); }
function getImagePath($image) { $image = str_replace("T", $this->resolution, $image); $img_path = $this->resolution . "/" . $image . ".PNG"; if (!$this->file_exists($this->get_file_path($img_path))) { $img_path = $this->resolution . "/" . $image . ".JPG"; } $number = str_replace($this->resolution, "", $image) * 1; $src_img = imagecreatefromstring($this->get_file_content($this->get_file_path($img_path))); $img = imagecreatetruecolor($this->getWidth($number), $this->getHeight($number)); ImageSaveAlpha($img, false); ImageAlphaBlending($img, false); imagefilledrectangle($img, 0, 0, $this->getWidth($number), $this->getHeight($number), imagecolorallocatealpha($img, 0, 0, 0, 127)); imagecopyresized($img, $src_img, 0, 0, 0, 0, $this->getWidth($number), $this->getHeight($number), imagesx($src_img), imagesy($src_img)); imagejpeg($img, "./temp/" . $image . ".jpg"); return realpath("./temp/" . $image . ".jpg"); }
/** * Based on the Watermark function by Marek Malcherek * http://www.malcherek.de * * @param string $color * @param string $wmFont * @param int $wmSize * @param int $wmOpaque */ function watermarkCreateText($color = '000000', $wmFont, $wmSize = 10, $wmOpaque = 90) { // set font path $wmFontPath = NGGALLERY_ABSPATH . "fonts/" . $wmFont; if (!is_readable($wmFontPath)) { return; } // This function requires both the GD library and the FreeType library. if (!function_exists('ImageTTFBBox')) { return; } $TextSize = @ImageTTFBBox($wmSize, 0, $wmFontPath, $this->watermarkText) or die; $TextWidth = abs($TextSize[2]) + abs($TextSize[0]); $TextHeight = abs($TextSize[7]) + abs($TextSize[1]); // Create Image for Text $this->workingImage = ImageCreateTrueColor($TextWidth, $TextHeight); ImageSaveAlpha($this->workingImage, true); ImageAlphaBlending($this->workingImage, false); $bgText = imagecolorallocatealpha($this->workingImage, 255, 255, 255, 127); imagefill($this->workingImage, 0, 0, $bgText); $wmTransp = 127 - $wmOpaque * 1.27; $rgb = $this->hex2rgb($color, false); $TextColor = imagecolorallocatealpha($this->workingImage, $rgb[0], $rgb[1], $rgb[2], $wmTransp); // Create Text on image imagettftext($this->workingImage, $wmSize, 0, 0, abs($TextSize[5]), $TextColor, $wmFontPath, $this->watermarkText); $this->watermarkImgPath = $this->workingImage; return; }
function WatermarkOverlay(&$gdimg_dest, &$img_watermark, $alignment = '*', $opacity = 50, $margin = 5) { if (is_resource($gdimg_dest) && is_resource($img_watermark)) { $watermark_source_x = 0; $watermark_source_y = 0; $img_source_width = ImageSX($gdimg_dest); $img_source_height = ImageSY($gdimg_dest); $watermark_source_width = ImageSX($img_watermark); $watermark_source_height = ImageSY($img_watermark); $watermark_opacity_percent = max(0, min(100, $opacity)); if ($margin < 1) { $watermark_margin_percent = 1 - $margin; } else { $watermark_margin_percent = (100 - max(0, min(100, $margin))) / 100; } $watermark_margin_x = round((1 - $watermark_margin_percent) * $img_source_width); $watermark_margin_y = round((1 - $watermark_margin_percent) * $img_source_height); switch ($alignment) { case '*': if ($gdimg_tiledwatermark = phpthumb_functions::ImageCreateFunction($img_source_width, $img_source_height)) { ImageAlphaBlending($gdimg_tiledwatermark, false); if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) { ImageSaveAlpha($gdimg_tiledwatermark, true); } $text_color_transparent = phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_tiledwatermark, 255, 0, 255, 127); ImageFill($gdimg_tiledwatermark, 0, 0, $text_color_transparent); // set the tiled image transparent color to whatever the untiled image transparency index is // ImageColorTransparent($gdimg_tiledwatermark, ImageColorTransparent($img_watermark)); // a "cleaner" way of doing it, but can't handle the margin feature :( // ImageSetTile($gdimg_tiledwatermark, $img_watermark); // ImageFill($gdimg_tiledwatermark, 0, 0, IMG_COLOR_TILED); // break; // ImageFill($gdimg_tiledwatermark, 0, 0, ImageColorTransparent($gdimg_tiledwatermark)); // tile the image as many times as can fit for ($x = $watermark_margin_x; $x < $img_source_width + $watermark_source_width; $x += round($watermark_source_width + (1 - $watermark_margin_percent) * $img_source_width)) { for ($y = $watermark_margin_y; $y < $img_source_height + $watermark_source_height; $y += round($watermark_source_height + (1 - $watermark_margin_percent) * $img_source_height)) { ImageCopy($gdimg_tiledwatermark, $img_watermark, $x, $y, 0, 0, min($watermark_source_width, $img_source_width - $x - (1 - $watermark_margin_percent) * $img_source_width), min($watermark_source_height, $img_source_height - $y - (1 - $watermark_margin_percent) * $img_source_height)); } } $watermark_source_width = ImageSX($gdimg_tiledwatermark); $watermark_source_height = ImageSY($gdimg_tiledwatermark); $watermark_destination_x = 0; $watermark_destination_y = 0; ImageDestroy($img_watermark); $img_watermark = $gdimg_tiledwatermark; } break; case 'T': $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2 + $watermark_margin_x); $watermark_destination_y = $watermark_margin_y; break; case 'B': $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2 + $watermark_margin_x); $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent); break; case 'L': $watermark_destination_x = $watermark_margin_x; $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2 + $watermark_margin_y); break; case 'R': $watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent); $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2 + $watermark_margin_y); break; case 'C': $watermark_destination_x = round($img_source_width / 2 - $watermark_source_width / 2); $watermark_destination_y = round($img_source_height / 2 - $watermark_source_height / 2); break; case 'TL': $watermark_destination_x = $watermark_margin_x; $watermark_destination_y = $watermark_margin_y; break; case 'TR': $watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent); $watermark_destination_y = $watermark_margin_y; break; case 'BL': $watermark_destination_x = $watermark_margin_x; $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent); break; case 'BR': default: $watermark_destination_x = round(($img_source_width - $watermark_source_width) * $watermark_margin_percent); $watermark_destination_y = round(($img_source_height - $watermark_source_height) * $watermark_margin_percent); break; } ImageAlphaBlending($gdimg_dest, false); if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=')) { ImageSaveAlpha($gdimg_dest, true); ImageSaveAlpha($img_watermark, true); } phpthumb_functions::ImageCopyRespectAlpha($gdimg_dest, $img_watermark, $watermark_destination_x, $watermark_destination_y, 0, 0, $watermark_source_width, $watermark_source_height, $watermark_opacity_percent); return true; } return false; }