<?php
// **************************************** // // 功能:图片叠加 // 参数: $dst 背景图片地址 // src 叠加图片地址 // newfile 另存图片文件名 // left 距离背景图片左边的距离 // top 距离背景图片上部的距离 // **************************************** // function superimposedPng($dst,$src,$newfile,$left=null,$top=null) { //得到原始图片信息 $dst_im = imagecreatefrompng($dst); $dst_info = getimagesize($dst); imagesavealpha($dst_im, true); //水印图像 $src_im = imagecreatefrompng($src); $src_info = getimagesize($src); if(empty($left)){ $left = $dst_info[0]-$src_info[0]; } if(empty($top)){ $top = $dst_info[1]-$src_info[1]; } //合并水印图片 imagecopy($dst_im,$src_im,$left,$top,0,0,$src_info[0],$src_info[1]); //输出合并后水印图片 imagepng($dst_im, $newfile); imagedestroy($dst_im); imagedestroy($src_im); } // **************************************** // // 功能:图片叠加 // 参数: $dst 背景图片地址 // src png图片地址 // size ico的大小 // filename 转换的ico的名字 // **************************************** // function covertPngToIco($src,$size,$filename) { $im = imagecreatefrompng($src); $imginfo = getimagesize($src); $resize_im = @imagecreatetruecolor($size,$size); imagealphablending($resize_im, false); imagecolortransparent($resize_im, imagecolorallocatealpha($resize_im, 0, 0, 0,0)); imagecopyresampled($resize_im,$im,0,0,0,0,$size,$size,$imginfo[0],$imginfo[1]); include "phpthumb.ico.php"; $icon = new phpthumb_ico(); $gd_image_array = array($resize_im); $icon_data = $icon->GD2ICOstring($gd_image_array); $filename = $filename.".ico"; //保存ico file_put_contents($filename, $icon_data); } // **************************************** // // 功能:重置图片大小 // 参数: $im 图片值 // maxwidth 转换长度 // maxheight 转换高度 // name 转换的名字 // filetype 转换类型 // **************************************** // function resizeImage($img, $w, $h, $newfilename) { //Check if GD extension is loaded if (!extension_loaded('gd') && !extension_loaded('gd2')) { trigger_error("GD is not loaded", E_USER_WARNING); return false; } //Get Image size info $imgInfo = getimagesize($img); switch ($imgInfo[2]) { case 1: $im = imagecreatefromgif($img); break; case 2: $im = imagecreatefromjpeg($img); break; case 3: $im = imagecreatefrompng($img); break; default: trigger_error('Unsupported filetype!', E_USER_WARNING); break; } //If image dimension is smaller, do not resize if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) { $nHeight = $imgInfo[1]; $nWidth = $imgInfo[0]; }else{ //yeah, resize it, but keep it proportional if ($w/$imgInfo[0] > $h/$imgInfo[1]) { $nWidth = $w; $nHeight = $imgInfo[1]*($w/$imgInfo[0]); }else{ $nWidth = $imgInfo[0]*($h/$imgInfo[1]); $nHeight = $h; } } $nWidth = round($nWidth); $nHeight = round($nHeight); $newImg = imagecreatetruecolor($nWidth, $nHeight); /* Check if this image is PNG or GIF, then set if Transparent*/ if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){ imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent); } imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]); //Generate the file, and rename it to $newfilename switch ($imgInfo[2]) { case 1: imagegif($newImg,$newfilename); break; case 2: imagejpeg($newImg,$newfilename); break; case 3: imagepng($newImg,$newfilename); break; default: trigger_error('Failed resize image!', E_USER_WARNING); break; } return $newfilename; } //原始图像 $logo = "su.png"; resizeImage("ok.png","30","30","ok.png"); superimposedPng($logo, "ok.png","su.png"); covertPngToIco("su.png",64,"ok"); ?>
在使用过程中参考网址,以方便各位参考
png透明处理 http://www.phpweblog.net/young40/archive/2008/11/26/6124.html
png图片变黑 http://hi.baidu.com/guantong_gt/blog/item/f35bbade73a6341d48540378.html
png生成ico http://www.jb51.net/article/12458.htm
png缩放图片 http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/