目录
生成缩略图之后的页面显示问题
php
缩略图
<code><?php header("content-type:text/html;charset=utf-8"); class Thumb{ // 水印图片路径 - 100*130 private $watermark; // 生成缩略图的路径 public $thumbPath="./"; public function __construct(){ $this->watermark="./logo.jpg"; } // $imageInfo是上传图片后返回的信息 // $type - 1代表生成商品或文章的缩略图,2代表生成头像缩略图 public function makeThumb($imageInfo,$with=360,$height=360,$type=1){ if($imageInfo['type']==".jpg"){ $src_image=imagecreatefromjpeg($imageInfo['source_img_path']); }else if($imageInfo['type']==".png"){ $src_image=imagecreatefrompng($imageInfo['source_img_path']); }else if($imageInfo['type']==".gif"){ $src_image=imagecreatefromgif($imageInfo['source_img_path']); } $image=imagecreatetruecolor($with,$height); $white=imagecolorallocate($image,255,255,255); // 将背景设为透明 imagecolortransparent($image,$white); imagefill($image,0,0,$white); // 开始判断最终生成的缩略图尺寸,避免变形 $finalWidth=$with; $finalHeight=$height; $x=$y=0; if($imageInfo['width']$imageInfo['height']){ // 确定缩放比例 $scale=round($finalWidth/$imageInfo['width'],2); $finalHeight=round($scale*$imageInfo['height']); $y=round(($height-$finalHeight)/2); }else if($imageInfo['width']$height){ $scale=round($finalHeight/$imageInfo['height'],2); $finalWidth=round($scale*$imageInfo['width']); $x=round(($with-$finalWidth)/2); }else if($with=200 && $finalHeight>=260){ $watermark=imagecreatefromjpeg($this->watermark); imagecopymerge($image,$watermark,$finalWidth-100,$finalHeight-130,0,0,100,130,80); imagedestroy($watermark); } !is_dir($this->thumbPath) && mkdir($this->thumbPath); // 保存路径+名字 $this->thumbPath=$this->thumbPath."/".time().mt_rand()."_{$finalWidth}_{$finalHeight}.png"; // 最后统一生成png格式,清晰度较高 header("content-type:image/png"); imagepng($image,$this->thumbPath); imagedestroy($image); imagedestroy($src_image); } } if($_FILES){ $a=new Thumb(); // 实际图片信息是上传处理过的信息,这里写成了固定的 $a->makeThumb(array('width'=>430,'height'=>430,'type'=>'.jpg','source_img_path'=>'./11.jpg')); } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="thumb"> <p></p> <input type="submit" name="" value="上 传"> <input type="reset" name="" value="重 置"> </form></code>
登录后复制
登录后复制
11.jpg
水印图
执行该脚本之后,生成的缩略图没问题,正常保存
但是页面却显示小边框
求解,谢谢诸位
回复内容:
<code><?php header("content-type:text/html;charset=utf-8"); class Thumb{ // 水印图片路径 - 100*130 private $watermark; // 生成缩略图的路径 public $thumbPath="./"; public function __construct(){ $this->watermark="./logo.jpg"; } // $imageInfo是上传图片后返回的信息 // $type - 1代表生成商品或文章的缩略图,2代表生成头像缩略图 public function makeThumb($imageInfo,$with=360,$height=360,$type=1){ if($imageInfo['type']==".jpg"){ $src_image=imagecreatefromjpeg($imageInfo['source_img_path']); }else if($imageInfo['type']==".png"){ $src_image=imagecreatefrompng($imageInfo['source_img_path']); }else if($imageInfo['type']==".gif"){ $src_image=imagecreatefromgif($imageInfo['source_img_path']); } $image=imagecreatetruecolor($with,$height); $white=imagecolorallocate($image,255,255,255); // 将背景设为透明 imagecolortransparent($image,$white); imagefill($image,0,0,$white); // 开始判断最终生成的缩略图尺寸,避免变形 $finalWidth=$with; $finalHeight=$height; $x=$y=0; if($imageInfo['width']$imageInfo['height']){ // 确定缩放比例 $scale=round($finalWidth/$imageInfo['width'],2); $finalHeight=round($scale*$imageInfo['height']); $y=round(($height-$finalHeight)/2); }else if($imageInfo['width']$height){ $scale=round($finalHeight/$imageInfo['height'],2); $finalWidth=round($scale*$imageInfo['width']); $x=round(($with-$finalWidth)/2); }else if($with=200 && $finalHeight>=260){ $watermark=imagecreatefromjpeg($this->watermark); imagecopymerge($image,$watermark,$finalWidth-100,$finalHeight-130,0,0,100,130,80); imagedestroy($watermark); } !is_dir($this->thumbPath) && mkdir($this->thumbPath); // 保存路径+名字 $this->thumbPath=$this->thumbPath."/".time().mt_rand()."_{$finalWidth}_{$finalHeight}.png"; // 最后统一生成png格式,清晰度较高 header("content-type:image/png"); imagepng($image,$this->thumbPath); imagedestroy($image); imagedestroy($src_image); } } if($_FILES){ $a=new Thumb(); // 实际图片信息是上传处理过的信息,这里写成了固定的 $a->makeThumb(array('width'=>430,'height'=>430,'type'=>'.jpg','source_img_path'=>'./11.jpg')); } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="thumb"> <p></p> <input type="submit" name="" value="上 传"> <input type="reset" name="" value="重 置"> </form></code>
登录后复制
登录后复制
11.jpg
水印图
执行该脚本之后,生成的缩略图没问题,正常保存
但是页面却显示小边框
求解,谢谢诸位
header("content-type:image/png");
把这一段删除试试?
你只是生成缩略图并不直接把缩略图显示给浏览器,却配置了浏览器输出为png格式,显示个方块,就是浏览器要显示图片,但是却没有正确的图片显示。
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
1 周前
By DDD
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
在哪里可以找到原子中的起重机控制钥匙卡
1 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu
