之前用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);
//$ak开发密钥,$cityname城市名称(支持省县乡详细地址) public function getposition($ak,$cityname){ $callback=array('lng'=>0,'l...
如果想在windows中执行php,并且让php脚本在后台运行,可以用下面的cmd命令start /b php D:\wwwroot\default\demo1\run.php例如上面的命令意思后台运行run.php,如果想用php编写异步代码: ...
xmlrpc协议是通过http请求xml数据进行通信。webservice中和它相同的是soap。soap调用的确很简单,但是创建wsdl太繁琐,效率低下。xmlrpc很好的解决这个问题。(1).创建xmlrpc服务端(求和函数api)function getSum($method,$ar...
<?php /** * @throws Exception */ function curl() { throw new \Exception('err...
<?php //如果支持exec函数,可以使用的方式 exec('chcp 65001'); //如果exec函数因安全问题禁用,可以使用的方式 pclose(popen('chcp 65001', 'r'));...
最近在项目中处理一个关于商品数据重复需要删除多余的商品记录,但是删除一条商品必然要把关联的其他表商品的id和其他商品信息更换为正确的,删除一个商品记录,同时要去修改100多张表的关联商品数据,在项目中引用了tp orm 1.2版本,由于项目是php5.6版本,没法使用最新orm,在代码中每处理1个商...