imagecopymerge() - gd函数(图像处理)
imagecopymerge()
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
拷贝并合并图像的一部分
说明
imagecopymerge (resource $dst_im,resource $src_im,int $dst_x,int $dst_y,int $src_x,int $src_y,int $src_w,int $src_h,int $pct) : bool 将$src_im图像中坐标从$src_x,src_y
开始,宽度为$src_w,高度为$src_h的一部分拷贝到$dst_im图像中坐标为$dst_x和$dst_y的位置上。两图像将根据$pct来决定合并程度,其值范围从 0 到 100。当$pct= 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
Note:
本函数是 PHP 4.0.6 新加的。
I've just checked PHP's issue tracker and a core developer says that this function was never meant to support alpha channel! and they refused to commit the provided patch! so ridicules Anyway i tried rodrigo's workaround and it worked quite well, thanks rodrigo for sharing it. I came up with another idea which is much faster than his solution, (it may need a little bit more memory) Hope it helps someone.
imagecopymerge PHP function helped me to create a fast method that replaces one color by another in true color images. Beforehand for this purpose I had to loop over all the pixels of an image and replace color pixel by pixel using imagecolorat and imagesetpixel; this method is really slow for large images. So here is the fast one:
Building upon backglancer's and stefan's posts below, the following script will lay a 24-bit PNG watermark over any image. To prepare a 24-bit watermark, I recommend creating a white logo or text over a transparent background in Photoshop. Save this as a 24-bit PNG via 'Save for the Web...'. Be sure to set the transparency of the logo layer in Photoshop itself. 30-40% is a good setting. Once the assets are prepared, throw the full or relative server paths at the watermark function below: /******************************************************************/ function watermark($sourcefile, $watermarkfile) { # # $sourcefile = Filename of the picture to be watermarked. # $watermarkfile = Filename of the 24-bit PNG watermark file. # //Get the resource ids of the pictures $watermarkfile_id = imagecreatefrompng($watermarkfile); imageAlphaBlending($watermarkfile_id, false); imageSaveAlpha($watermarkfile_id, true); $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3)); switch($fileType) { case('gif'): $sourcefile_id = imagecreatefromgif($sourcefile); break; case('png'): $sourcefile_id = imagecreatefrompng($sourcefile); break; default: $sourcefile_id = imagecreatefromjpeg($sourcefile); } //Get the sizes of both pix $sourcefile_width=imageSX($sourcefile_id); $sourcefile_height=imageSY($sourcefile_id); $watermarkfile_width=imageSX($watermarkfile_id); $watermarkfile_height=imageSY($watermarkfile_id); $dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 ); $dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 ); // if a gif, we have to upsample it to a truecolor image if($fileType == 'gif') { // create an empty truecolor container $tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height); // copy the 8-bit gif into the truecolor image imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height); // copy the source_id int $sourcefile_id = $tempimage; } imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0, $watermarkfile_width, $watermarkfile_height); //Create a jpeg out of the modified picture switch($fileType) { // remember we don't need gif any more, so we use only png or jpeg. // See the upsaple code immediately above to see how we handle gifs case('png'): header("Content-type: image/png"); imagepng ($sourcefile_id); break; default: header("Content-type: image/jpg"); imagejpeg ($sourcefile_id); } imagedestroy($sourcefile_id); imagedestroy($watermarkfile_id); }
killing_wombles0000 gave an almost working piece of code to mirror an image with increasing transparency. With help from showdev on stackexchange, here's the corrected and fully working code:
The following function creates mask of a true color image for a given color. Beforehand for creating an image mask I used to loop over all image pixels and check their color using imagecolorat and copy if the color matches with imagesetpixel. This was very slow for large images, so the following code improves the process. So for example, if we have a photo and we specify color = (255, 0, 0), i.e. red, the result will be image of the same size with red pixels everywhere the original photo was red and grey pixels exerywhere else.
Some highly amusing stuff: This code appears normal and is a way to cut out slices of an image randomly. Try running this however and an omitted detail adds to the fun. Since the first colour that is defined is taken to be the background colour, we have a random colour selected from $origim as the background colour for $imagetodisplay, but this random colour is weighted according to the background image. It was a surprise (I thought the bg would be black) but I am now keeping it as it looks good.
I was about to kill myself.... any one of you trying to merge a SEMI transparent png... use imagecopy :) ImageSaveAlpha(resource, bool); made the transparent color - not transparent... dunno why :)
I needed to draw a "pointer" image over a map, but had some problems with png image transparency. So I created a png image with white background (not transparent) and merged it on my map, after defining white color as transparent:
This function is intended to serve as an example for the "imageCopyMerge"-"ImageCopyResized", "ImageColorTransparent" functions. I hope it will help. This function pick an image, square cut it resampled at the size requested and finishing merge the result with another one to obtain a circular image result. I have a problem to obtain an thumbnail that respect the colors and at the same time where cutted in a circular form, I hope this solution can gives you a clue to obtain a free form images, I use this one also to create multicutted images.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)