之前用ceomax的wordpress主题发现它的缩略图很好看,于是其他项目也是参考它的来生成图片。
背景图如下:
原图如下:
生成效果图:
参考代码:
// 使用示例 $originalImagePath = './944b687112.png'; $backgroundImagePath = './ceo_img_1.png'; $thumbnailWidth = 200; $thumbnailHeight = 200; $thumbnailQuality = 75; // 加载图像 $background = imagecreatefrompng($backgroundImagePath); $foreground = imagecreatefrompng($originalImagePath); // 获取图像尺寸 $backgroundWidth = imagesx($background); $backgroundHeight = imagesy($background); $foregroundOriginalWidth = imagesx($foreground); $foregroundOriginalHeight = imagesy($foreground); // 计算缩放比例 $scaleX = $backgroundWidth / $foregroundOriginalWidth; $scaleY = $backgroundHeight / $foregroundOriginalHeight; $scale = min($scaleX, $scaleY); // 选择较小的比例以保证图像完全适应背景 // 计算缩放后的尺寸 $foregroundWidth = 282; $foregroundHeight = 166; // 创建一个新图像,大小与背景图像相同 $newImage = imagecreatetruecolor($backgroundWidth, $backgroundHeight); // 复制背景图像到新图像 imagecopy($newImage, $background, 0, 0, 0, 0, $backgroundWidth, $backgroundHeight); // 计算前景图像在新图像中的位置(这里居中) $x = 59; $y = 56; // 使用imagecopyresampled缩放并复制前景图像到新图像 imagecopyresampled($newImage, $foreground, $x, $y, 0, 0, $foregroundWidth, $foregroundHeight, $foregroundOriginalWidth, $foregroundOriginalHeight); // 保存新图像 imagejpeg($newImage, 'new_image.jpg', 100); // 90是质量参数,范围从0到100 // 释放资源 imagedestroy($background); imagedestroy($foreground); imagedestroy($newImage);
<?php //对比$this和self /* * $this更倾向于对象本身 * */ class Par{ public  ...
应用场景:PHP模拟购买,商品数量大于0才能购买常见代码:<?php //连接数据库 $con=mysqli_connect("localhost","ihuohuo","927464cy","ihuohuo");...
php多进程应用场景主要是非web端,fpm下是不支持多进程的,非类linux操作系统都不支持,请在cli模式使用.可以使用多进程做任务分发,批量计算,批量文件处理,批量爬虫,网络运维等等。下面看一份简单的入门demo//创建子进程 $pid=pcntl_fork(); //返回-1,创建失败,不...
项目中使用服务来执行webservice,由于对方系统api不稳定,经常导致服务崩溃,只能重启,一个月差不多要重启一次。初期的解决办法是捕获异常,然后continue掉。<?php try { $url = 'http...
PHP不像net支持多继承,自身只支持单继承,为了解决这个问题,php出了Trait这个特性,减少单继承语言的限制。并且能让代码复用率更高。说白了就是一个对象的属性和方法扩展工具一样。例如:trait exts { public f...
ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...