function waterMark($fileInHD, $wmFile, $transparency = 50, $jpegQuality = 90, $margin = 5) { $wmImg = imageCreateFromJPEG($wmFile); $jpegImg = imageCreateFromJPEG($fileInHD); // Water mark random position $wmX = (bool) rand(0, 1) ? $margin : imageSX($jpegImg) - imageSX($wmImg) - $margin; $wmY = (bool) rand(0, 1) ? $margin : imageSY($jpegImg) - imageSY($wmImg) - $margin; // Water mark process imageCopyMerge($jpegImg, $wmImg, $wmX, $wmY, 0, 0, imageSX($wmImg), imageSY($wmImg), $transparency); // Overwriting image ImageJPEG($jpegImg, $fileInHD, $jpegQuality); }
/** * Creates an image for every code character. */ protected function createCharImages() { $counter = 0; for ($i = 0, $j = strlen($this->codeWord); $i < $j; $i++) { $char = $this->codeWord[$i]; $tempImageWidth = $this->fontSize * 2; $tempImageHeight = $this->fontSize + 40; // create image $tempImage = imageCreate($tempImageWidth, $tempImageHeight); $tempColor = imageColorAllocate($tempImage, $this->color2['red'], $this->color2['green'], $this->color2['blue']); imageColorTransparent($tempImage, $tempColor); // set font color $fontColor = imageColorAllocate($tempImage, $this->fontColor['red'], $this->fontColor['green'], $this->fontColor['blue']); // write text imageTtfText($tempImage, $this->fontSize, 0, 10, $this->fontSize + mt_rand(25, 30), $fontColor, $this->fontFace, $char); // morph text if (CAPTCHA_FONT_MORPH) { $tempImageHeight = 120; $tempImage2 = imageCreate($tempImageWidth, 120); $tempColor = imageColorAllocate($tempImage2, $this->color2['red'], $this->color2['green'], $this->color2['blue']); imageColorTransparent($tempImage2, $tempColor); $divisor = mt_rand(6, 7); $quotient = mt_rand(4, 6); $method = mt_rand(0, 1); // morph text on x-axis if ($method == 0) { for ($y = 1; $y <= $tempImageHeight; $y++) { $posX = sin($y / $divisor) * $quotient; imageCopyMerge($tempImage2, $tempImage, $posX, $y, 0, $y, $tempImageWidth, 1, 100); } } // morph text on y-axis if ($method == 1) { for ($x = 1; $x <= $tempImageWidth; $x++) { $posY = sin($x / $divisor) * $quotient; imageCopyMerge($tempImage2, $tempImage, $x, $posY, $x, 0, 1, $tempImageHeight, 100); } } $image = $tempImage2; } else { $image = $tempImage; } // get text width and height $positionX = 0; for ($x = $tempImageWidth - 1; $x > 0; $x--) { for ($y = $tempImageHeight - 1; $y > 0; $y--) { $color = imageColorAt($image, $x, $y); $colorArray = imageColorsForIndex($image, $color); if ($colorArray['red'] == $this->fontColor['red'] && $colorArray['green'] == $this->fontColor['green'] && $colorArray['blue'] == $this->fontColor['blue']) { $positionX = $x; $x = 0; $y = 0; break; } } } $width = $positionX + 10; $height = 100; // create final char image $this->chars[$counter] = imageCreate($width, $height); $color2 = imageColorAllocate($this->chars[$counter], $this->color2['red'], $this->color2['green'], $this->color2['blue']); imageColorTransparent($this->chars[$counter], $color2); imageCopyMerge($this->chars[$counter], $image, 5, 5, 0, 0, $width, $tempImageHeight, 100); $this->charWidth[$counter] = $width; // destroy temp images imageDestroy($tempImage); if (CAPTCHA_FONT_MORPH) { imageDestroy($tempImage2); } $counter++; } }
private function marcaDagua() { // Obtém o cabeçalho de ambas as imagens // http://br3.php.net/manual/pt_BR/function.imagecreatefrompng.php $cab_marca = imagecreatefrompng($this->img_marca); // http://br3.php.net/manual/pt_BR/function.imagecreatefromjpeg.php $cab_imagem = imagecreatefromjpeg($this->pasta . "/" . $this->nome); // Obtém os tamanhos de ambas as imagens // http://br3.php.net/manual/pt_BR/function.getimagesize.php $tam_imagem = getimagesize($this->pasta . "/" . $this->nome); $tam_marca = getimagesize($this->img_marca); $largura_img = $tam_imagem[0]; $altura_img = $tam_imagem[1]; $largura_marca = $tam_marca[0]; $altura_marca = $tam_marca[1]; // Aqui, defini-se a posição onde a marca deve aparecer na foto: Rodapé Direito $eixo_x = $largura_img - $largura_marca - 5; $eixo_y = $altura_img - $altura_marca - 5; // http://br3.php.net/manual/pt_BR/function.imagecolortransparent.php // http://br3.php.net/manual/pt_BR/function.imagecolorallocate.php imagecolortransparent($cab_marca, imagecolorallocate($cab_marca, 4, 137, 193)); // A função principal: misturar as duas imagens imageCopyMerge($cab_imagem, $cab_marca, $eixo_x, $eixo_y, 0, 0, $largura_marca, $altura_marca, 50); // Cria a imagem com a marca da agua // http://br3.php.net/manual/pt_BR/function.getimagesize.php imagejpeg($cab_imagem, $this->pasta . "/" . $this->nome, 90); }
function Watermark_GD($preview = 0) { global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext; $watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus; if ($watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) { $imagecreatefromfunc = $this->imagecreatefromfunc; $imagefunc = $this->imagefunc; list($img_w, $img_h) = $this->attachinfo; if ($watermarktype < 2) { $watermark_file = $watermarktype == 1 ? './images/common/watermark.png' : './images/common/watermark.gif'; $watermarkinfo = @getimagesize($watermark_file); $watermark_logo = $watermarktype == 1 ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file); if (!$watermark_logo) { return; } list($logo_w, $logo_h) = $watermarkinfo; } else { $watermarktextcvt = pack("H*", $watermarktext['text']); $box = imagettfbbox($watermarktext['size'], $watermarktext['angle'], $watermarktext['fontpath'], $watermarktextcvt); $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]); $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]); $ax = min($box[0], $box[6]) * -1; $ay = min($box[5], $box[7]) * -1; } $wmwidth = $img_w - $logo_w; $wmheight = $img_h - $logo_h; if (($watermarktype < 2 && is_readable($watermark_file) || $watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) { switch ($watermarkstatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($img_w - $logo_w) / 2; $y = +5; break; case 3: $x = $img_w - $logo_w - 5; $y = +5; break; case 4: $x = +5; $y = ($img_h - $logo_h) / 2; break; case 5: $x = ($img_w - $logo_w) / 2; $y = ($img_h - $logo_h) / 2; break; case 6: $x = $img_w - $logo_w; $y = ($img_h - $logo_h) / 2; break; case 7: $x = +5; $y = $img_h - $logo_h - 5; break; case 8: $x = ($img_w - $logo_w) / 2; $y = $img_h - $logo_h - 5; break; case 9: $x = $img_w - $logo_w - 5; $y = $img_h - $logo_h - 5; break; } $dst_photo = imagecreatetruecolor($img_w, $img_h); $target_photo = @$imagecreatefromfunc($this->targetfile); imageCopy($dst_photo, $target_photo, 0, 0, 0, 0, $img_w, $img_h); if ($watermarktype == 1) { imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); } elseif ($watermarktype == 2) { if (($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor']) { $shadowcolorrgb = explode(',', $watermarktext['shadowcolor']); $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]); imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax + $watermarktext['shadowx'], $y + $ay + $watermarktext['shadowy'], $shadowcolor, $watermarktext['fontpath'], $watermarktextcvt); } $colorrgb = explode(',', $watermarktext['color']); $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]); imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax, $y + $ay, $color, $watermarktext['fontpath'], $watermarktextcvt); } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans); } $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT . './forumdata/watermark_temp.jpg'; clearstatcache(); if ($this->attachinfo['mime'] == 'image/jpeg') { $imagefunc($dst_photo, $targetfile, $watermarkquality); } else { $imagefunc($dst_photo, $targetfile); } $this->attach['size'] = filesize($targetfile); } } }
function Watermark_GD() { if (function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) { $imagecreatefromfunc = $this->imagecreatefromfunc; $imagefunc = $this->imagefunc; list($img_w, $img_h) = $this->attachinfo; if ($this->watermarktype < 2) { //非文本 $watermark_file = HDWIKI_ROOT . './style/default/watermark/logo.' . ($this->watermarktype == 1 ? 'png' : 'gif'); $watermarkinfo = @getimagesize($watermark_file); $watermark_logo = $this->watermarktype == 1 ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file); if (!$watermark_logo) { return; } list($logo_w, $logo_h) = $watermarkinfo; } else { //水印是文本类型 $watermarktextcvt = $this->watermarktext['text']; $box = imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'], $watermarktextcvt); $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]); $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]); $ax = min($box[0], $box[6]) * -1; $ay = min($box[5], $box[7]) * -1; } $wmwidth = $img_w - $logo_w; $wmheight = $img_h - $logo_h; if (($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) { switch ($this->watermarkstatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($img_w - $logo_w) / 2; $y = +5; break; case 3: $x = $img_w - $logo_w - 5; $y = +5; break; case 4: $x = +5; $y = ($img_h - $logo_h) / 2; break; case 5: $x = ($img_w - $logo_w) / 2; $y = ($img_h - $logo_h) / 2; break; case 6: $x = $img_w - $logo_w - 5; $y = ($img_h - $logo_h) / 2; break; case 7: $x = +5; $y = $img_h - $logo_h - 5; break; case 8: $x = ($img_w - $logo_w) / 2; $y = $img_h - $logo_h - 5; break; case 9: $x = $img_w - $logo_w - 5; $y = $img_h - $logo_h - 5; break; } $dst_photo = imagecreatetruecolor($img_w, $img_h); $target_photo = @$imagecreatefromfunc($this->srcfile); imageCopy($dst_photo, $target_photo, 0, 0, 0, 0, $img_w, $img_h); if ($this->watermarktype == 1) { imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); } elseif ($this->watermarktype == 2) { if (($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor']) { $shadowcolorrgb = $this->excolor($this->watermarktext['shadowcolor']); $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]); imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'], $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor, $this->watermarktext['fontpath'], $watermarktextcvt); } $colorrgb = $this->excolor($this->watermarktext['color']); $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]); imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'], $x + $ax, $y + $ay, $color, $this->watermarktext['fontpath'], $watermarktextcvt); } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $this->watermarktrans); } if ($this->attachinfo['mime'] == 'image/jpeg') { $imagefunc($dst_photo, $this->targetfile, $this->watermarkquality); } else { $imagefunc($dst_photo, $this->targetfile); } } else { return false; } } return true; }
/** * 给图片增加水印(使用GD库) * @return void */ function waterMark($targetFile = '', $upFile = '') { if ($targetFile) { $this->image($targetFile, $upFile); } if ($this->waterMarkMinWidth && $this->targetInfo[0] <= $this->waterMarkMinWidth && $this->waterMarkMinHeight && $this->targetInfo[1] <= $this->waterMarkMinHeight || $this->waterMarkType == 2 && (!file_exists($this->waterMarkText['fontpath']) || !is_file($this->waterMarkText['fontpath']))) { return; } if ($this->waterMarkStatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) { $imageCreateFromFunc = $this->imageCreateFromFunc; $imageFunc = $this->imageFunc; list($imgWidth, $imgHeight) = $this->targetInfo; if ($this->waterMarkType < 2) { $waterMarkFile = $this->waterMarkType == 1 ? MOOPHP_ROOT . '/' . $this->waterImagePath . 'watermark.png' : MOOPHP_ROOT . '/' . $this->waterImagePath . 'watermark.gif'; $waterMarkInfo = @getimagesize($waterMarkFile); $waterMarkLogo = $this->waterMarkType == 1 ? @imageCreateFromPNG($waterMarkFile) : @imageCreateFromGIF($waterMarkFile); if (!$waterMarkLogo) { return; } list($logoWidth, $logoHeight) = $waterMarkInfo; } $wmwidth = $imgWidth - $logoWidth; $wmheight = $imgHeight - $logoHeight; if (($this->waterMarkType < 2 && is_readable($waterMarkFile) || $this->waterMarkType == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedGif) { switch ($this->waterMarkStatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($imgWidth - $logoWidth) / 2; $y = +5; break; case 3: $x = $imgWidth - $logoWidth - 5; $y = +5; break; case 4: $x = +5; $y = ($imgHeight - $logoHeight) / 2; break; case 5: $x = ($imgWidth - $logoWidth) / 2; $y = ($imgHeight - $logoHeight) / 2; break; case 6: $x = $imgWidth - $logoWidth; $y = ($imgHeight - $logoHeight) / 2; break; case 7: $x = +5; $y = $imgHeight - $logoHeight - 5; break; case 8: $x = ($imgWidth - $logoWidth) / 2; $y = $imgHeight - $logoHeight - 5; break; case 9: $x = $imgWidth - $logoWidth - 5; $y = $imgHeight - $logoHeight - 5; break; } $destinationPhoto = imagecreatetruecolor($imgWidth, $imgHeight); $targetPhoto = @$imageCreateFromFunc($this->targetFile); imageCopy($destinationPhoto, $targetPhoto, 0, 0, 0, 0, $imgWidth, $imgHeight); if ($this->waterMarkType == 1) { imageCopy($destinationPhoto, $waterMarkLogo, $x, $y, 0, 0, $logoWidth, $logoHeight); } else { imageAlphaBlending($waterMarkLogo, true); imageCopyMerge($destinationPhoto, $waterMarkLogo, $x, $y, 0, 0, $logoWidth, $logoHeight, $this->waterMarkTrans); } clearstatcache(); if ($this->targetInfo['mime'] == 'image/jpeg') { $imageFunc($destinationPhoto, $this->targetFile, $this->waterMarkQuality); } else { $imageFunc($destinationPhoto, $this->targetFile); } $this->upFile['size'] = filesize($this->targetFile); } } }
function Watermark_GD($type = 'forum') { if (!function_exists('imagecreatetruecolor')) { return -4; } $imagefunc =& $this->imagefunc; if ($this->param['watermarktype'][$type] != 'text') { if (!function_exists('imagecopy') || !function_exists('imagecreatefrompng') || !function_exists('imagecreatefromgif') || !function_exists('imagealphablending') || !function_exists('imagecopymerge')) { return -4; } $watermarkinfo = @getimagesize($this->param['watermarkfile'][$type]); if ($watermarkinfo === FALSE) { return -3; } $watermark_logo = $this->param['watermarktype'][$type] == 'png' ? @imageCreateFromPNG($this->param['watermarkfile'][$type]) : @imageCreateFromGIF($this->param['watermarkfile'][$type]); if (!$watermark_logo) { return 0; } list($logo_w, $logo_h) = $watermarkinfo; } else { if (!function_exists('imagettfbbox') || !function_exists('imagettftext') || !function_exists('imagecolorallocate')) { return -4; } if (!class_exists('Chinese')) { include libfile('class/chinese'); } $watermarktextcvt = pack("H*", $this->param['watermarktext']['text'][$type]); $box = imagettfbbox($this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt); $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]); $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]); $ax = min($box[0], $box[6]) * -1; $ay = min($box[5], $box[7]) * -1; } $wmwidth = $this->imginfo['width'] - $logo_w; $wmheight = $this->imginfo['height'] - $logo_h; if ($wmwidth > 10 && $wmheight > 10 && !$this->imginfo['animated']) { switch ($this->param['watermarkstatus'][$type]) { case 1: $x = 5; $y = 5; break; case 2: $x = ($this->imginfo['width'] - $logo_w) / 2; $y = 5; break; case 3: $x = $this->imginfo['width'] - $logo_w - 5; $y = 5; break; case 4: $x = 5; $y = ($this->imginfo['height'] - $logo_h) / 2; break; case 5: $x = ($this->imginfo['width'] - $logo_w) / 2; $y = ($this->imginfo['height'] - $logo_h) / 2; break; case 6: $x = $this->imginfo['width'] - $logo_w; $y = ($this->imginfo['height'] - $logo_h) / 2; break; case 7: $x = 5; $y = $this->imginfo['height'] - $logo_h - 5; break; case 8: $x = ($this->imginfo['width'] - $logo_w) / 2; $y = $this->imginfo['height'] - $logo_h - 5; break; case 9: $x = $this->imginfo['width'] - $logo_w - 5; $y = $this->imginfo['height'] - $logo_h - 5; break; } if ($this->imginfo['mime'] != 'image/png') { $color_photo = imagecreatetruecolor($this->imginfo['width'], $this->imginfo['height']); } $dst_photo = $this->loadsource(); if ($dst_photo < 0) { return $dst_photo; } imagealphablending($dst_photo, true); imagesavealpha($dst_photo, true); if ($this->imginfo['mime'] != 'image/png') { imageCopy($color_photo, $dst_photo, 0, 0, 0, 0, $this->imginfo['width'], $this->imginfo['height']); $dst_photo = $color_photo; } if ($this->param['watermarktype'][$type] == 'png') { imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); } elseif ($this->param['watermarktype'][$type] == 'text') { if (($this->param['watermarktext']['shadowx'][$type] || $this->param['watermarktext']['shadowy'][$type]) && $this->param['watermarktext']['shadowcolor'][$type]) { $shadowcolorrgb = explode(',', $this->param['watermarktext']['shadowcolor'][$type]); $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]); imagettftext($dst_photo, $this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $x + $ax + $this->param['watermarktext']['shadowx'][$type], $y + $ay + $this->param['watermarktext']['shadowy'][$type], $shadowcolor, $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt); } $colorrgb = explode(',', $this->param['watermarktext']['color'][$type]); $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]); imagettftext($dst_photo, $this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $x + $ax, $y + $ay, $color, $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt); } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $this->param['watermarktrans'][$type]); } clearstatcache(); if ($this->imginfo['mime'] == 'image/jpeg') { @$imagefunc($dst_photo, $this->target, $this->param['watermarkquality'][$type]); } else { @$imagefunc($dst_photo, $this->target); } } return 1; }
private function thumb_gd() { $this->initi_img(); if (!is_resource($this->im)) { return false; } $this->dst_img(); $this->width = imagesx($this->im); $this->height = imagesy($this->im); /** * 改变后的图象的比例 */ $resize_ratio = $this->resize_width / $this->resize_height; /** * 实际图象的比例 */ $ratio = $this->width / $this->height; if ($this->scale > 0 && $this->filling) { $wh = $this->scale; // if ($this->resize_height > $this->resize_width){ // $wh = $this->resize_height; // }else{ // $wh = $this->resize_width; // } if ($ratio > 1) { $dst_y = ($wh - $wh / $ratio) / 2; $dst_x = 0; } else { $dst_x = ($wh - $wh * $ratio) / 2; $dst_y = 0; } //等比缩图(宽高最大为$wh) if ($resize_ratio > 1) { $s_width = $wh; $s_height = $wh / $resize_ratio; } else { $s_height = $wh; $s_width = $wh * $resize_ratio; } $snewimg = imagecreatetruecolor($s_width, $s_height); imagecopyresampled($snewimg, $this->im, 0, 0, 0, 0, $s_width, $s_height, $this->width, $this->height); // $snewimg = imagecreatetruecolor($this->resize_width,$this->resize_height); // imagecopyresampled($snewimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->height); //创建空白正方目标图 $newimg = imagecreatetruecolor($wh, $wh); $white = imagecolorallocate($newimg, 255, 255, 255); imagefill($newimg, 0, 0, $white); //填入图片 imageCopyMerge($newimg, $snewimg, $dst_x, $dst_y, 0, 0, $s_width, $s_height, 100); // imageCopyMerge($newimg, $snewimg,$dst_x,$dst_y,0,0,$this->resize_width,$this->resize_height,100); // imageCopyMerge($newimg, $snewimg,$dst_x,$dst_y,0,0,$this->width*$wh/$this->height,$wh,100); $foreground_color = imagecolorallocate($newimg, 255, 0, 0); ImageJpeg($newimg, $this->dstimg, $this->config['thumb_quality']); ImageDestroy($snewimg); } else { /** * 不裁图 */ if ($ratio > 1) { //宽大高小 $this->resize_height = $this->resize_width * $this->height / $this->width; } else { $this->resize_width = $this->resize_height * $this->width / $this->height; } $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->height); ImageJpeg($newimg, $this->dstimg, $this->config['thumb_quality']); } /** * 销毁临时图 */ ImageDestroy($this->im); ImageDestroy($newimg); }
/** * Add a Water Mark to the image * (filigrana) * * @param string $from * @param string $waterMark */ public function addWaterMarkImage($waterMark, $opacity = 35, $x = 5, $y = 5) { // set data $size = $this->info; $im = $this->gdID; // set WaterMark's data $waterMarkSM = new SmartImage($waterMark); $imWM = $waterMarkSM->getGDid(); // Add it! // In png watermark images we ignore the opacity (you have to set it in the watermark image) if ($waterMarkSM->info[2] == 3) { imageCopy($im, $imWM, $x, $y, 0, 0, imagesx($imWM), imagesy($imWM)); } else { imageCopyMerge($im, $imWM, $x, $y, 0, 0, imagesx($imWM), imagesy($imWM), $opacity); } $waterMarkSM->close(); $this->gdID = $im; }
/** * 添加水印 * * @access public * @param mixed $sourcefile * @param int $position * @return mixed */ public function waterMark($sourcefile, $position = 0) { if ($sourcefile != '' && file_exists($sourcefile)) { list($imgWidth, $imgHeight, $imgtype) = getImageSize($sourcefile); $sourceImage = $this->createImage($imgtype, $sourcefile); } else { return '图片文件不存在!'; } $isWaterImage = false; if ($this->waterImage !== null && file_exists($this->waterImage)) { $isWaterImage = true; list($waterWidth, $waterHeight, $watertype) = getImageSize($this->waterImage); $waterImage = $this->createImage($watertype, $this->waterImage); } //水印位置 if ($isWaterImage) { //图片水印 $w = $waterWidth; $h = $waterHeight; } else { if (file_exists($this->fontFile)) { //文字水印 $temp = imagettfbbox($this->fontSize, 0, $this->fontFile, $this->waterText); //取得使用 TrueType 字体的文本的范围 $w = $temp[2] - $temp[0]; $h = $temp[1] - $temp[7]; unset($temp); } else { if ($this->fontSize > 5 || $this->fontSize < 1) { $this->fontSize = 5; } $base = ceil($this->fontSize * 2.54); //10+$this->fontSize<<2; $w = $base * strlen($this->waterText); $h = $base; //10+$this->fontSize<<1; } } if ($imgWidth < $w || $imgHeight < $h) { $w = $imgWidth; $h = $imgHeight; } switch ($position) { case 0: // $posX = rand(0, $imgWidth - $w); $posY = rand(0, $imgHeight - $h); break; case 1: //1为顶端居左 $posX = 0; $posY = 0; break; case 2: //2为顶端居中 $posX = ($imgWidth - $w) / 2; $posY = 0; break; case 3: //3为顶端居右 $posX = $imgWidth - $w; $posY = 0; break; case 4: //4为中部居左 $posX = 0; $posY = ($imgHeight - $h) / 2; break; case 5: //5为中部居中 $posX = ($imgWidth - $w) / 2; $posY = ($imgHeight - $h) / 2; break; case 6: //6为中部居右 $posX = $imgWidth - $w; $posY = ($imgHeight - $h) / 2; break; case 7: //7为底端居左 $posX = 0; $posY = $imgHeight - $h; break; case 8: //8为底端居中 $posX = ($imgWidth - $w) / 2; $posY = $imgHeight - $h; break; case 9: //9为底端居右 $posX = $imgWidth - $w; $posY = $imgHeight - $h; break; default: //随机 $posX = rand(0, $imgWidth - $w); $posY = rand(0, $imgHeight - $h); break; } //设定图像的混色模式 imagealphablending($sourceImage, true); if ($isWaterImage) { //图片水印 imageCopyMerge($sourceImage, $waterImage, $posX, $posY, 0, 0, $waterWidth, $waterHeight, $this->alpha); } else { //文字水印 if (!empty($this->fontColor) && strlen($this->fontColor) == 7) { $R = hexdec(substr($this->fontColor, 1, 2)); $G = hexdec(substr($this->fontColor, 3, 2)); $B = hexdec(substr($this->fontColor, 5)); } else { $R = hexdec('00'); $G = hexdec('00'); $B = hexdec('00'); } if (file_exists($this->fontFile)) { imagettftext($sourceImage, $this->fontSize, 0, $posX, $h + $posY, imagecolorallocate($sourceImage, $R, $G, $B), $this->fontFile, $this->waterText); } else { imagestring($sourceImage, $this->fontSize, $posX, $posY - $h, $this->waterText, imagecolorallocate($sourceImage, $R, $G, $B)); } } //生成水印后的图片 //@unlink($sourcefile); switch ($imgtype) { //取得背景图片的格式 case 1: imagegif($sourceImage, $sourcefile); break; case 2: imagejpeg($sourceImage, $sourcefile); break; case 3: imagepng($sourceImage, $sourcefile); break; default: die($errorMsg); } //释放内存 if (isset($waterImage)) { imagedestroy($waterImage); } if (isset($sourceImage)) { imagedestroy($sourceImage); } }
function create($filename = "") { if ($filename) { $this->src_image_name = strtolower(trim($filename)); } if (!file_exists($this->src_image_name)) { return false; } if ($this->wm_image_pos == 99) { return $this->save_image_file; } $src_image_type = $this->get_type($this->src_image_name); $src_image = $this->createImage($src_image_type, $this->src_image_name); if (!$src_image) { return; } $src_image_w = ImageSX($src_image); $src_image_h = ImageSY($src_image); if ($this->wm_image_name && file_exists($this->wm_image_name)) { $this->wm_image_name = strtolower(trim($this->wm_image_name)); $wm_image_type = $this->get_type($this->wm_image_name); $wm_image = $this->createImage($wm_image_type, $this->wm_image_name); if (!@($wm_image_w = ImageSX($wm_image))) { return false; } if (!@($wm_image_h = ImageSY($wm_image))) { return false; } if ($this->wm_image_dest_x && $this->wm_image_dest_y) { $wm_image_x = $this->wm_image_dest_x; $wm_image_y = $this->wm_image_dest_y; } else { $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image); $wm_image_x = $temp_wm_image["dest_x"]; $wm_image_y = $temp_wm_image["dest_y"]; } imageCopyMerge($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h, $this->wm_image_transition); } if ($this->wm_text) { $this->wm_text = $this->gb2utf8($this->wm_text); if ($this->wm_text_dest_x && $this->wm_text_dest_y) { //绝对定位 $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_text_pos); $wm_text_x = $temp_wm_text["dest_x"]; //$this->wm_text_dest_x; $wm_text_y = $this->wm_text_dest_y; } else { $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_text_pos); $wm_text_x = $temp_wm_text["dest_x"]; $wm_text_y = $temp_wm_text["dest_y"]; } if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $wm_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $wm_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->wm_text_size, $this->wm_text_angle, $wm_text_x, $wm_text_y, $wm_text_color, $this->wm_text_font, $this->wm_text); } if ($this->save_image_file) { switch ($this->output_type) { case 'gif': $src_img = ImagePNG($src_image, $this->save_image_file); break; case 'jpeg': $src_img = ImageJPEG($src_image, $this->save_image_file, $this->jpeg_quality); break; case 'png': $src_img = ImagePNG($src_image, $this->save_image_file); break; default: $src_img = ImageJPEG($src_image, $this->save_image_file, $this->jpeg_quality); break; } } else { if ($src_image_type = "jpg") { $src_image_type = "jpeg"; } header("Content-type: image/{$src_image_type}"); switch ($src_image_type) { case 'gif': $src_img = imagegif($src_image); break; case 'jpg': $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 'png': $src_img = ImagePNG($src_image); break; default: $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); return $this->save_image_file; }
function mergePix($bgPath, $srcPath, $destPath, $pos = 0, $transition = 100) { //Get the resource id´s of the pictures $srcPath_id = imageCreateFromJPEG($srcPath); $bgPath_id = imageCreateFromJPEG($bgPath); //Get the sizes of both pix $bgPath_width = imageSX($bgPath_id); $bgPath_height = imageSY($bgPath_id); $srcPath_width = imageSX($srcPath_id); $srcPath_height = imageSY($srcPath_id); //middle if ($pos == 0) { $dest_x = $bgPath_width / 2 - $srcPath_width / 2; $dest_y = $bgPath_height / 2 - $srcPath_height / 2; } //top left if ($pos == 1) { $dest_x = 0; $dest_y = 0; } //top right if ($pos == 2) { $dest_x = $bgPath_width - $srcPath_width; $dest_y = 0; } //bottom right if ($pos == 3) { $dest_x = $bgPath_width - $srcPath_width; $dest_y = $bgPath_height - $srcPath_height; } //bottom left if ($pos == 4) { $dest_x = 0; $dest_y = $bgPath_height - $srcPath_height; } //top middle if ($pos == 5) { $dest_x = ($bgPath_width - $srcPath_width) / 2; $dest_y = 0; } //middle right if ($pos == 6) { $dest_x = $bgPath_width - $srcPath_width; $dest_y = $bgPath_height / 2 - $srcPath_height / 2; } //bottom middle if ($pos == 7) { $dest_x = ($bgPath_width - $srcPath_width) / 2; $dest_y = $bgPath_height - $srcPath_height; } //middle left if ($pos == 8) { $dest_x = 0; $dest_y = $bgPath_height / 2 - $srcPath_height / 2; } //The main thing : merge the two pix imageCopyMerge($bgPath_id, $srcPath_id, $dest_x, $dest_y, 0, 0, $srcPath_width, $srcPath_height, $transition); //Create a jpeg out of the modified picture imagejpeg($bgPath_id, "{$destPath}", 100); }
<?php /* $Id$ */ if (!defined('IMG_GIF')) { $rank = file_exists("rank" . $_GET['rank'] . ".gif") ? $_GET['rank'] : "bg"; header("HTTP/1.1 301 Moved Permanently"); header("Location: rank{$rank}.gif"); exit; } else { header('Content-type: image/gif'); $bg = @imageCreateFromGIF("rankbg.gif"); $fg = @imageCreateFromGIF("rank{$_GET['rank']}.gif"); if (!$bg && !$fg) { exit; } // Null image, but who cares? Wrong is wrong is wrong. if (!$bg) { imageGIF($fg); exit; } if ($fg) { imageCopyMerge($bg, $fg, 0, 0, 0, 0, 17, 17, 100); } imageGIF($bg); }
/** * 生成增加水印的图片 * * <code> * //使用示例 * <?php * $img = new GDImage(); * //文字水印 * $img->wm_text = "www.shopnc.net"; * $img->wm_text_font = "./STXINWEI.TTF"; * //图片水印 * $img->wm_image_name="水印图片名"; * $img->create("生成图片文件名(包括路径)"); * ?> * </code> * * @param string $filename 需要增加水印的图片文件名 * @return bool 布尔类型的返回结果 */ public function create($filename = "") { @ini_set('memory_limit', '-1'); if ($filename) { $this->src_image_name = strtolower(trim($filename)); $this->save_file = $this->src_image_name; } $src_image_type = $this->get_type($this->src_image_name); $src_image = $this->createImage($src_image_type, $this->src_image_name); if (!$src_image) { return false; } $src_image_w = ImageSX($src_image); $src_image_h = ImageSY($src_image); if ($this->wm_image_name) { $this->wm_image_name = strtolower(trim($this->wm_image_name)); $wm_image_type = $this->get_type($this->wm_image_name); $wm_image = $this->createImage($wm_image_type, $this->wm_image_name); $wm_image_w = ImageSX($wm_image); $wm_image_h = ImageSY($wm_image); $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image); $wm_image_x = $temp_wm_image["dest_x"]; $wm_image_y = $temp_wm_image["dest_y"]; imageCopyMerge($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h, $this->wm_image_transition); } if ($this->wm_text) { $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_text_pos); $wm_text_x = $temp_wm_text["dest_x"]; $wm_text_y = $temp_wm_text["dest_y"]; if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $wm_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $wm_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, $this->wm_text_font, $this->wm_text); } if ($this->save_file) { switch ($this->get_type($this->save_file)) { case 'gif': $src_img = imagegif($src_image, $this->save_file); break; case 'jpeg': $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; case 'jpg': $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; case 'png': $src_img = ImagePNG($src_image, $this->save_file); break; default: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; } } else { if ($src_image_type == "jpg") { $src_image_type = "jpeg"; } @header("Content-type: image/{$src_image_type}"); switch ($src_image_type) { case 'gif': $src_img = imagegif($src_image); break; case 'jpg': $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 'jpeg': $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 'png': $src_img = ImagePNG($src_image); break; default: $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); return true; }
public function watermark($source, $name = '') { if (!($info = $this->info($source))) { return false; } if ($this->params['watermarktype'] == 'png') { if (!function_exists('imagecopy') || !function_exists('imagecreatefrompng') || !function_exists('imagealphablending') || !function_exists('imagecopymerge')) { return -4; } $watermarkfile = __CFG::DIR . 'data/watermark/watermark.png'; $watermarkinfo = @getimagesize($watermarkfile); if ($watermarkinfo === FALSE) { return -3; } if (!($watermark_logo = @imageCreateFromPNG($watermarkfile))) { return 0; } list($logo_w, $logo_h) = $watermarkinfo; } else { if ($this->params['watermarktype'] == 'text') { if (!function_exists('imagettfbbox') || !function_exists('imagettftext') || !function_exists('imagecolorallocate')) { return -4; } $font_file = __CFG::DIR . 'data/watermark/' . $this->params['watermarktext']['font']; $font_size = $this->params['watermarktext']['size'] ? $this->params['watermarktext']['size'] : '14'; if (!file_exists($font_file)) { return -5; } $font_text = str_replace(array("\n", "\r", "'"), array('', '', '\''), $this->params['watermarktext']['text']); $font_text = str_replace('{name}', $name, $font_text); //$font_text = pack("H*", $text); $box = imagettfbbox($font_size, 0, $font_file, $font_text); $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]); $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]); $ax = min($box[0], $box[6]) * -1; $ay = min($box[5], $box[7]) * -1; } else { return -6; } } $wmwidth = $info['width'] - $logo_w; $wmheight = $info['height'] - $logo_h; if ($wmwidth > 10 && $wmheight > 10) { switch ($this->params['watermarkstatus']) { case 1: $x = 5; $y = 5; break; case 2: $x = ($info['width'] - $logo_w) / 2; $y = 5; break; case 3: $x = $info['width'] - $logo_w - 5; $y = 5; break; case 4: $x = 5; $y = ($info['height'] - $logo_h) / 2; break; case 5: $x = ($info['width'] - $logo_w) / 2; $y = ($info['height'] - $logo_h) / 2; break; case 6: $x = $info['width'] - $logo_w; $y = ($info['height'] - $logo_h) / 2; break; case 7: $x = 5; $y = $info['height'] - $logo_h - 5; break; case 8: $x = ($info['width'] - $logo_w) / 2; $y = $info['height'] - $logo_h - 5; break; case 9: default: $x = $info['width'] - $logo_w - 5; $y = $info['height'] - $logo_h - 5; break; } if (!($dst_photo = $this->loadsource($source, $info['mime']))) { return false; } imagealphablending($dst_photo, true); imagesavealpha($dst_photo, true); if ($info['mime'] != 'image/png') { $color_photo = imagecreatetruecolor($info['width'], $info['height']); imageCopy($color_photo, $dst_photo, 0, 0, 0, 0, $info['width'], $info['height']); $dst_photo = $color_photo; } if ($this->params['watermarktype'] == 'png') { imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); } else { if ($this->params['watermarktype'] == 'text') { $colorrgb = sscanf($this->params['watermarktext']['color'], '#%2x%2x%2x'); $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]); @imagettftext($dst_photo, $font_size, 0, $x + $ax, $y + $ay, $color, $font_file, $font_text); } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $this->params['watermarktrans']); } } clearstatcache(); if ($info['mime'] == 'image/jpeg') { $watermarkquality = $this->params['watermarkquality'] ? (int) $this->params['watermarkquality'] : 90; @Imagejpeg($dst_photo, $source, $watermarkquality); } else { @$this->save($dst_photo, $source, $info['mime']); } } return 1; }
function makewatermark($srcfile) { global $_SCONFIG; if ($_SCONFIG['watermark'] && function_exists('imageCreateFromJPEG') && function_exists('imageCreateFromPNG') && function_exists('imageCopyMerge')) { $srcfile = A_DIR . '/' . $srcfile; $watermark_file = $_SCONFIG['watermarkfile']; $watermarkstatus = $_SCONFIG['watermarkstatus']; $fileext = fileext($watermark_file); $ispng = $fileext == 'png' ? true : false; $attachinfo = @getimagesize($srcfile); if (!empty($attachinfo) && is_array($attachinfo) && $attachinfo[2] != 1 && $attachinfo['mime'] != 'image/gif') { } else { return ''; } $watermark_logo = $ispng ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file); if (!$watermark_logo) { return ''; } $logo_w = imageSX($watermark_logo); $logo_h = imageSY($watermark_logo); $img_w = $attachinfo[0]; $img_h = $attachinfo[1]; $wmwidth = $img_w - $logo_w; $wmheight = $img_h - $logo_h; if (is_readable($watermark_file) && $wmwidth > 100 && $wmheight > 100) { switch ($attachinfo['mime']) { case 'image/jpeg': $dst_photo = imageCreateFromJPEG($srcfile); break; case 'image/gif': $dst_photo = imageCreateFromGIF($srcfile); break; case 'image/png': $dst_photo = imageCreateFromPNG($srcfile); break; default: break; } switch ($watermarkstatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($img_w - $logo_w) / 2; $y = +5; break; case 3: $x = $img_w - $logo_w - 5; $y = +5; break; case 4: $x = +5; $y = ($img_h - $logo_h) / 2; break; case 5: $x = ($img_w - $logo_w) / 2; $y = ($img_h - $logo_h) / 2; break; case 6: $x = $img_w - $logo_w - 5; $y = ($img_h - $logo_h) / 2; break; case 7: $x = +5; $y = $img_h - $logo_h - 5; break; case 8: $x = ($img_w - $logo_w) / 2; $y = $img_h - $logo_h - 5; break; case 9: $x = $img_w - $logo_w - 5; $y = $img_h - $logo_h - 5; break; } if ($ispng) { $watermark_photo = imagecreatetruecolor($img_w, $img_h); imageCopy($watermark_photo, $dst_photo, 0, 0, 0, 0, $img_w, $img_h); imageCopy($watermark_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); $dst_photo = $watermark_photo; } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $_SCONFIG['watermarktrans']); } switch ($attachinfo['mime']) { case 'image/jpeg': imageJPEG($dst_photo, $srcfile, $_SCONFIG['watermarkjpgquality']); break; case 'image/gif': imageGIF($dst_photo, $srcfile); break; case 'image/png': imagePNG($dst_photo, $srcfile); break; } } } }
function mergePix($sourcefile, $insertfile, $pos = 0, $transition = 50, $path, $unreg_quality, $cache, $pic_filetype, $file) { //Get the resource id´s of the pictures $insertfile_id = imageCreateFromPNG($insertfile); if ($file == 1) { if ($pic_filetype == '.jpg') { $sourcefile_id = imageCreateFromJPEG($sourcefile); } elseif ($pic_filetype == '.png') { $sourcefile_id = ImageCreateFromPNG($sourcefile); } else { $sourcefile_id = imageCreateFromJPEG($sourcefile); } } else { $sourcefile_id = $sourcefile; } //Get the sizes of both pix $sourcefile_width = imageSX($sourcefile_id); $sourcefile_height = imageSY($sourcefile_id); $insertfile_width = imageSX($insertfile_id); $insertfile_height = imageSY($insertfile_id); //middle if ($pos == 0) { $dest_x = $sourcefile_width / 2 - $insertfile_width / 2; $dest_y = $sourcefile_height / 2 - $insertfile_height / 2; } //top left if ($pos == 1) { $dest_x = 0; $dest_y = 0; } //top right if ($pos == 2) { $dest_x = $sourcefile_width - $insertfile_width; $dest_y = 0; } //bottom right if ($pos == 3) { $dest_x = $sourcefile_width - $insertfile_width; $dest_y = $sourcefile_height - $insertfile_height; } //bottom left if ($pos == 4) { $dest_x = 0; $dest_y = $sourcefile_height - $insertfile_height; } //top middle if ($pos == 5) { $dest_x = ($sourcefile_width - $insertfile_width) / 2; $dest_y = 0; } //middle right if ($pos == 6) { $dest_x = $sourcefile_width - $insertfile_width; $dest_y = $sourcefile_height / 2 - $insertfile_height / 2; } //bottom middle if ($pos == 7) { $dest_x = ($sourcefile_width - $insertfile_width) / 2; $dest_y = $sourcefile_height - $insertfile_height; } //middle left if ($pos == 8) { $dest_x = 0; $dest_y = $sourcefile_height / 2 - $insertfile_height / 2; } //The main thing : merge the two pix imageCopyMerge($sourcefile_id, $insertfile_id, $dest_x, $dest_y, 0, 0, $insertfile_width, $insertfile_height, $transition); //Create a jpeg out of the modified picture // imagejpeg ($sourcefile_id,"$targetfile"); // Imagejpeg($sourcefile_id,'',100); if ($cache == 1) { @imagejpeg($sourcefile_id, $path, $unreg_quality); @imagejpeg($sourcefile_id, '', $unreg_quality); } else { @imagejpeg($sourcefile_id, '', $unreg_quality); } @ImageDestroy($sourcefile_id); }
function Thumb_GD($thumbwidth, $thumbheight) { if (function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) { $imagecreatefromfunc = $this->imagecreatefromfunc; $imagefunc = $this->imagefunc; list($img_w, $img_h) = $this->attachinfo; if (!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) { $attach_photo = $imagecreatefromfunc($this->srcfile); $imgratio = $img_w / $img_h; $thumbratio = $thumbwidth / $thumbheight; if ($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) { $cuty = $img_h; $cutx = $cuty * $thumbratio; } elseif ($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) { $cutx = $img_w; $cuty = $cutx / $thumbratio; } $dst_photo = imagecreatetruecolor($cutx, $cuty); imageCopyMerge($dst_photo, $attach_photo, 0, 0, 0, 0, $cutx, $cuty, 100); $thumb['width'] = $thumbwidth; $thumb['height'] = $thumbheight; $targetfile = $this->targetfile; $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']); imageCopyreSampled($thumb_photo, $dst_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $cutx, $cuty); clearstatcache(); if ($this->attachinfo['mime'] == 'image/jpeg') { $imagefunc($thumb_photo, $targetfile, 100); } else { $imagefunc($thumb_photo, $targetfile); } return TRUE; } } return FALSE; }
/** * 给图片添加水印 * @param filepath $src 待处理图片 * @param filepath $mark_img 水印图片路径 * @param string $position 水印位置 lt左上 rt右上 rb右下 lb左下 其余取值为中间 * @param int $quality jpg图片质量,仅对jpg有效 默认85 取值 0-100之间整数 * @param int $pct 水印图片融合度(透明度) * * @return void */ function water_mark($src, $mark_img, $position = 'rb', $quality = 85, $pct = 80) { if (function_exists('imagecopy') && function_exists('imagecopymerge')) { $data = getimagesize($src); if ($data[2] > 3) { return false; } $src_width = $data[0]; $src_height = $data[1]; $src_type = $data[2]; $data = getimagesize($mark_img); $mark_width = $data[0]; $mark_height = $data[1]; $mark_type = $data[2]; if ($src_width < $mark_width + 20 || $src_width < $mark_height + 20) { return false; } switch ($src_type) { case 1: $src_im = imagecreatefromgif($src); $imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : ''; break; case 2: $src_im = imagecreatefromjpeg($src); $imagefunc = function_exists('imagegif') ? 'imagejpeg' : ''; break; case 3: $src_im = imagecreatefrompng($src); $imagefunc = function_exists('imagepng') ? 'imagejpeg' : ''; break; } switch ($mark_type) { case 1: $mark_im = imagecreatefromgif($mark_img); break; case 2: $mark_im = imagecreatefromjpeg($mark_img); break; case 3: $mark_im = imagecreatefrompng($mark_img); break; } switch ($position) { case 'lt': $x = 10; $y = 10; break; case 'rt': $x = $src_width - $mark_width - 10; $y = 10; break; case 'rb': $x = $src_width - $mark_width - 10; $y = $src_height - $mark_height - 10; break; case 'lb': $x = 10; $y = $src_height - $mark_height - 10; break; default: $x = ($src_width - $mark_width - 10) / 2; $y = ($src_height - $mark_height - 10) / 2; break; } if (function_exists('imagealphablending')) { imageAlphaBlending($mark_im, true); } imageCopyMerge($src_im, $mark_im, $x, $y, 0, 0, $mark_width, $mark_height, $pct); if ($src_type == 2) { $imagefunc($src_im, $src, $quality); } else { $imagefunc($dst_photo, $src); } } }
function watermark($target, $watermark_file, $ext, $watermarkstatus = 9, $watermarktrans = 50) { $gdsurporttype = array(); if (function_exists('imageAlphaBlending') && function_exists('getimagesize')) { if (function_exists('imageGIF')) { $gdsurporttype[] = 'gif'; } if (function_exists('imagePNG')) { $gdsurporttype[] = 'png'; } if (function_exists('imageJPEG')) { $gdsurporttype[] = 'jpg'; $gdsurporttype[] = 'jpeg'; } } if ($gdsurporttype && in_array($ext, $gdsurporttype)) { $attachinfo = getimagesize($target); $watermark_logo = imageCreateFromGIF($watermark_file); $logo_w = imageSX($watermark_logo); $logo_h = imageSY($watermark_logo); $img_w = $attachinfo[0]; $img_h = $attachinfo[1]; $wmwidth = $img_w - $logo_w; $wmheight = $img_h - $logo_h; $animatedgif = 0; if ($attachinfo['mime'] == 'image/gif') { $fp = fopen($target, 'rb'); $targetcontent = fread($fp, 9999999); fclose($fp); $animatedgif = strpos($targetcontent, 'NETSCAPE2.0') === FALSE ? 0 : 1; } if ($watermark_logo && $wmwidth > 10 && $wmheight > 10 && !$animatedgif) { switch ($attachinfo['mime']) { case 'image/jpeg': $dst_photo = imageCreateFromJPEG($target); break; case 'image/gif': $dst_photo = imageCreateFromGIF($target); break; case 'image/png': $dst_photo = imageCreateFromPNG($target); break; } switch ($watermarkstatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($logo_w + $img_w) / 2; $y = +5; break; case 3: $x = $img_w - $logo_w - 5; $y = +5; break; case 4: $x = +5; $y = ($logo_h + $img_h) / 2; break; case 5: $x = ($logo_w + $img_w) / 2; $y = ($logo_h + $img_h) / 2; break; case 6: $x = $img_w - $logo_w; $y = ($logo_h + $img_h) / 2; break; case 7: $x = +5; $y = $img_h - $logo_h - 5; break; case 8: $x = ($logo_w + $img_w) / 2; $y = $img_h - $logo_h; break; case 9: $x = $img_w - $logo_w - 5; $y = $img_h - $logo_h - 5; break; } imageAlphaBlending($watermark_logo, FALSE); imagesavealpha($watermark_logo, TRUE); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans); switch ($attachinfo['mime']) { case 'image/jpeg': imageJPEG($dst_photo, $target); break; case 'image/gif': imageGIF($dst_photo, $target); break; case 'image/png': imagePNG($dst_photo, $target); break; } } } }
function addtagborder2($oim, $x, $y, $i, $startx, $starty, $outputsize, $shiftx, $shifty, $fuzzy) { $width = imagesX($oim); $height = imagesY($oim); if ($fuzzy < 54 * 2) { $addx = 54 * 2 + 4; } else { $addx = 0; } if ($fuzzy < 22 * 2) { $addy = 22 * 2 + 4; } else { $addy = 0; } error_log(sprintf("addtagborder2: original: %d x %d => new size: %d x %d\n", $width, $height, $width + $addx, $height + $addy)); $im = imagecreate($width + $addx, $height + $addy); $white = imageColorAllocate($im, 0xff, 0xff, 0xff); imageCopyMerge($im, $oim, 55, 23, 0, 0, imagesX($oim), imagesY($oim), 100); // 不需要算序號 $tagx = $startx; $tagy = $starty; if ($i < $x) { // add top $topim = tagXline($tagx, $outputsize['x']); imageCopyMerge($im, $topim, 0, 0, 0, 0, imagesX($topim), imagesY($topim), 100); imagedestroy($topim); } if ($i >= $x * ($y - 1)) { // add buttom $topim = tagXline($tagx, $outputsize['x']); $ozy = $outputsize['y']; $tty = 22 + ($shifty % $ozy == 0 ? $ozy : $shifty % $ozy) * 315 + 2; // echo "tty = $tty $tileY % $ozy \n"; imageCopyMerge($im, $topim, 0, $tty, 0, 0, imagesX($topim), imagesY($topim), 100); imagedestroy($topim); } if ($i % $x == 0) { // add left $topim = tagYline($tagy, $outputsize['y']); imageCopyMerge($im, $topim, 0, 0, 0, 0, imagesX($topim), imagesY($topim), 100); imagedestroy($topim); } if (($i + 1) % $x == 0) { // add right $topim = tagYline($tagy, $outputsize['y']); $ozx = $outputsize['x']; $ttx = 54 + ($shiftx % $ozx == 0 ? $ozx : $shiftx % $ozx) * 315 + 2; // echo "ttx = $ttx $tileX % $ozx \n"; imageCopyMerge($im, $topim, $ttx, 0, 0, 0, imagesX($topim), imagesY($topim), 100); imagedestroy($topim); } return $im; }
function makeThumbWatermark($width = 128, $height = 128) { $this->fileCheck(); $image_info = $this->getInfo($this->src_image_name); if (!$image_info) { return false; } $src_image_type = $image_info["type"]; $img = $this->createImage($src_image_type, $this->src_image_name); if (!$img) { return false; } $width = $width == 0 ? $image_info["width"] : $width; $height = $height == 0 ? $image_info["height"] : $height; $width = $width > $image_info["width"] ? $image_info["width"] : $width; $height = $height > $image_info["height"] ? $image_info["height"] : $height; $srcW = $image_info["width"]; $srcH = $image_info["height"]; if ($srcH * $width > $srcW * $height) { $width = round($srcW * $height / $srcH); } else { $height = round($srcH * $width / $srcW); } //* $src_image = @imagecreatetruecolor($width, $height); $white = @imagecolorallocate($src_image, 0xff, 0xff, 0xff); @imagecolortransparent($src_image, $white); @imagefilltoborder($src_image, 0, 0, $white, $white); if ($src_image) { ImageCopyResampled($src_image, $img, 0, 0, 0, 0, $width, $height, $image_info["width"], $image_info["height"]); } else { $src_image = imagecreate($width, $height); ImageCopyResized($src_image, $img, 0, 0, 0, 0, $width, $height, $image_info["width"], $image_info["height"]); } $src_image_w = ImageSX($src_image); $src_image_h = ImageSY($src_image); if ($this->wm_image_name) { $wm_image_info = $this->getInfo($this->wm_image_name); if (!$wm_image_info) { return false; } $wm_image_type = $wm_image_info["type"]; $wm_image = $this->createImage($wm_image_type, $this->wm_image_name); $wm_image_w = ImageSX($wm_image); $wm_image_h = ImageSY($wm_image); $temp_wm_image = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos, $wm_image); if ($this->emboss && function_exists("imagefilter")) { imagefilter($wm_image, IMG_FILTER_EMBOSS); $bgcolor = imagecolorclosest($wm_image, 0x7f, 0x7f, 0x7f); imagecolortransparent($wm_image, $bgcolor); } if (function_exists("ImageAlphaBlending") && IMAGETYPE_PNG == $wm_image_info['type']) { ImageAlphaBlending($src_image, true); } $wm_image_x = $temp_wm_image["dest_x"]; $wm_image_y = $temp_wm_image["dest_y"]; if (IMAGETYPE_PNG == $wm_image_info['type']) { imageCopy($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h); } else { imageCopyMerge($src_image, $wm_image, $wm_image_x, $wm_image_y, 0, 0, $wm_image_w, $wm_image_h, $this->wm_image_transition); } } if ($this->wm_text) { $this->wm_text = $this->wm_text; $temp_wm_text = $this->getPos($src_image_w, $src_image_h, $this->wm_image_pos); $wm_text_x = $temp_wm_text["dest_x"]; $wm_text_y = $temp_wm_text["dest_y"]; if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color)) { $red = hexdec($color[1]); $green = hexdec($color[2]); $blue = hexdec($color[3]); $wm_text_color = imagecolorallocate($src_image, $red, $green, $blue); } else { $wm_text_color = imagecolorallocate($src_image, 255, 255, 255); } imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color, $this->wm_text_font, $this->wm_text); } if ($this->save_file) { switch ($src_image_type) { case 1: if ($this->gif_enable) { $src_img = ImageGIF($src_image, $this->save_file); } else { $src_img = ImagePNG($src_image, $this->save_file); } break; case 2: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; case 3: $src_img = ImagePNG($src_image, $this->save_file); break; default: $src_img = ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break; } } else { switch ($src_image_type) { case 1: if ($this->gif_enable) { header("Content-type: image/gif"); $src_img = ImageGIF($src_image); } else { header("Content-type: image/png"); $src_img = ImagePNG($src_image); } break; case 2: header("Content-type: image/jpeg"); $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; case 3: header("Content-type: image/png"); $src_img = ImagePNG($src_image); break; case 6: header("Content-type: image/bmp"); $src_img = imagebmp($src_image); break; default: header("Content-type: image/jpeg"); $src_img = ImageJPEG($src_image, "", $this->jpeg_quality); break; } } imagedestroy($src_image); imagedestroy($img); return true; }
function Watermark_GD() { global $_F; if (!function_exists('imagecreatetruecolor')) { return -4; } $imageFunc =& $this->imageFunc; if ($this->param['watermarktype'] != 'text') { if (!function_exists('imagecopy') || !function_exists('imagecreatefrompng') || !function_exists('imagecreatefromgif') || !function_exists('imagealphablending') || !function_exists('imagecopymerge')) { return -4; } $watermarkinfo = getimagesize($this->param['watermarkfile']); if ($watermarkinfo === FALSE) { return -3; } $watermark_logo = $this->param['watermarktype'] == 'png' ? imageCreateFromPNG($this->param['watermarkfile']) : imageCreateFromGIF($this->param['watermarkfile']); if (!$watermark_logo) { return 0; } list($logo_w, $logo_h) = $watermarkinfo; } else { if (!function_exists('imagettfbbox') || !function_exists('imagettftext') || !function_exists('imagecolorallocate')) { return -4; } $watermarktextcvt = $this->param['watermarktext']['text']; //'';//pack("H*", $this->param['watermarktext']['text']); $box = imagettfbbox($this->param['watermarktext']['size'], $this->param['watermarktext']['angle'], $this->param['watermarktext']['fontpath'], $watermarktextcvt); $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]); $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]); $ax = min($box[0], $box[6]) * -1; $ay = min($box[5], $box[7]) * -1; } $wmwidth = $this->img_info['width'] - $logo_w; $wmheight = $this->img_info['height'] - $logo_h; //var_dump($wmwidth, $wmheight); // echo 'asdfasdf';exit; // 不管 图片多小都打logo if (1 || $wmwidth > 10 && $wmheight > 10 && !$this->img_info['animated']) { switch ($this->param['watermarkstatus']) { case 1: $x = 5; $y = 5; break; case 2: $x = ($this->img_info['width'] - $logo_w) / 2; $y = 5; break; case 3: $x = $this->img_info['width'] - $logo_w - 5; $y = 5; break; case 4: $x = 5; $y = ($this->img_info['height'] - $logo_h) / 2; break; case 5: $x = ($this->img_info['width'] - $logo_w) / 2; $y = ($this->img_info['height'] - $logo_h) / 2; break; case 6: $x = $this->img_info['width'] - $logo_w; $y = ($this->img_info['height'] - $logo_h) / 2; break; case 7: $x = 5; $y = $this->img_info['height'] - $logo_h - 5; break; case 8: $x = ($this->img_info['width'] - $logo_w) / 2; $y = $this->img_info['height'] - $logo_h - 5; break; case 9: $x = $this->img_info['width'] - $logo_w - 5; $y = $this->img_info['height'] - $logo_h - 5; break; } $dst_photo = imagecreatetruecolor($this->img_info['width'], $this->img_info['height']); $target_photo = $this->loadsource(); if ($target_photo < 0) { return $target_photo; } imageCopy($dst_photo, $target_photo, 0, 0, 0, 0, $this->img_info['width'], $this->img_info['height']); if ($this->param['watermarktype'] == 'png') { imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); } elseif ($this->param['watermarktype'] == 'text') { if (($this->param['watermarktext']['shadowx'] || $this->param['watermarktext']['shadowy']) && $this->param['watermarktext']['shadowcolor']) { $shadowcolorrgb = explode(',', $this->param['watermarktext']['shadowcolor']); $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]); imagettftext($dst_photo, $this->param['watermarktext']['size'], $this->param['watermarktext']['angle'], $x + $ax + $this->param['watermarktext']['shadowx'], $y + $ay + $this->param['watermarktext']['shadowy'], $shadowcolor, $this->param['watermarktext']['fontpath'], $watermarktextcvt); } $colorrgb = explode(',', $this->param['watermarktext']['color']); $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]); imagettftext($dst_photo, $this->param['watermarktext']['size'], $this->param['watermarktext']['angle'], $x + $ax, $y + $ay, $color, $this->param['watermarktext']['fontpath'], $watermarktextcvt); } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $this->param['watermarktrans']); } clearstatcache(); if ($this->img_info['mime'] == 'images/jpeg') { $imageFunc($dst_photo, $this->target, $this->param['watermarkquality']); } else { $imageFunc($dst_photo, $this->target); } } return 1; }