如何使用PHP中的GD库来调整,作物和水印图像?
PHP的GD库支持图像处理操作无需额外依赖。1.调整大小:使用imagecreatefromjpeg()加载图片,创建新尺寸画布,通过imagecopyresampled()缩放并保存;2.裁剪:加载原图后新建目标尺寸画布,复制指定区域;3.添加水印:可用imagettftext()加文字或用imagecopy()叠加透明PNG标志。基本功能简单有效,复杂需求可考虑其他库。
Resizing, cropping, and adding watermarks to images are common tasks when handling image uploads in PHP. The GD library, which is usually enabled by default in most PHP environments, gives you the tools to do all of that without relying on external libraries or services.
Here’s how to do each step with basic code examples and explanations.
Resize an Image Using GD
Resizing is useful for thumbnails or optimizing upload size. You can scale images proportionally or set exact dimensions.
To resize:
- Load the source image
- Create a blank canvas of the target size
- Use
imagecopyresampled()
to maintain quality - Save and free memory
$source = imagecreatefromjpeg('photo.jpg'); $width = imagesx($source); $height = imagesy($source); $newWidth = 200; $newHeight = 200; $resized = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($resized, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagejpeg($resized, 'resized_photo.jpg', 90); imagedestroy($source); imagedestroy($resized);
You can adjust this for PNGs using imagecreatefrompng()
and imagepng()
if needed.
Crop an Image to Specific Dimensions
Cropping is often used to generate square thumbnails or focus on a specific part of an image.
The process involves:
- Loading the original image
- Creating a new image resource with the desired crop size
- Copying only the selected portion using
imagecopyresampled()
$source = imagecreatefromjpeg('resized_photo.jpg'); $cropWidth = 150; $cropHeight = 150; $cropped = imagecreatetruecolor($cropWidth, $cropHeight); imagecopyresampled($cropped, $source, 0, 0, 50, 50, $cropWidth, $cropHeight, $cropWidth, $cropHeight); imagejpeg($cropped, 'cropped_photo.jpg', 90); imagedestroy($source); imagedestroy($cropped);
In this example, we're cropping starting from (50,50), which cuts off parts of the edges. Adjust those values based on where you want to focus.
Add a Watermark to an Image
Watermarking protects your content or adds branding. You can use text or another image as a watermark.
Text-based watermark:
$im = imagecreatefromjpeg('cropped_photo.jpg'); $black = imagecolorallocate($im, 0, 0, 0); $text = 'MySite.com'; // Add shadow effect imagettftext($im, 20, 0, 11, 21, $black, 'arial.ttf', $text); // Add main text $white = imagecolorallocate($im, 255, 255, 255); imagettftext($im, 20, 0, 10, 20, $white, 'arial.ttf', $text); imagejpeg($im, 'watermarked_photo.jpg', 90); imagedestroy($im);
Image-based watermark:
If you want to overlay a logo:
$main = imagecreatefromjpeg('photo.jpg'); $watermark = imagecreatefrompng('logo.png'); $wm_width = imagesx($watermark); $wm_height = imagesy($watermark); // Place watermark at bottom-right corner imagecopy($main, $watermark, imagesx($main) - $wm_width - 10, imagesy($main) - $wm_height - 10, 0, 0, $wm_width, $wm_height); imagejpeg($main, 'watermarked_with_logo.jpg', 90); imagedestroy($main); imagedestroy($watermark);
Make sure the watermark PNG has transparency so it blends well.
These techniques work well for simple image manipulation needs. If you’re dealing with many images or need more advanced features like filters or batch processing, consider looking into libraries like Imagine or ImageMagick.
But for resizing, cropping, and basic watermarking? GD gets the job done — no extra setup required in most cases.
基本上就这些。
以上是如何使用PHP中的GD库来调整,作物和水印图像?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

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

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

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

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

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

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

Dreamweaver CS6
视觉化网页开发工具

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

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

避免N 1查询问题,通过提前加载关联数据来减少数据库查询次数;2.仅选择所需字段,避免加载完整实体以节省内存和带宽;3.合理使用缓存策略,如Doctrine的二级缓存或Redis缓存高频查询结果;4.优化实体生命周期,定期调用clear()释放内存以防止内存溢出;5.确保数据库索引存在并分析生成的SQL语句以避免低效查询;6.在无需跟踪变更的场景下禁用自动变更跟踪,改用数组或轻量模式提升性能。正确使用ORM需结合SQL监控、缓存、批量处理和适当优化,在保持开发效率的同时确保应用性能。

要构建弹性的PHP微服务,需使用RabbitMQ实现异步通信,1.通过消息队列解耦服务,避免级联故障;2.配置持久化队列、持久化消息、发布确认和手动ACK以确保可靠性;3.使用指数退避重试、TTL和死信队列安全处理失败;4.通过supervisord等工具守护消费者进程并启用心跳机制保障服务健康;最终实现系统在故障中持续运作的能力。

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

使用正确的PHP基础镜像并配置安全、性能优化的Docker环境是实现生产就绪的关键。1.选用php:8.3-fpm-alpine作为基础镜像以减少攻击面并提升性能;2.通过自定义php.ini禁用危险函数、关闭错误显示并启用Opcache及JIT以增强安全与性能;3.使用Nginx作为反向代理,限制访问敏感文件并正确转发PHP请求至PHP-FPM;4.采用多阶段构建优化镜像,移除开发依赖,设置非root用户运行容器;5.可选Supervisord管理多个进程如cron;6.部署前验证无敏感信息泄

Bref使PHP开发者能无需管理服务器即可构建可扩展、成本高效的应用。1.Bref通过提供优化的PHP运行时层,将PHP带入AWSLambda,支持PHP8.3等版本,并与Laravel、Symfony等框架无缝集成;2.部署步骤包括:使用Composer安装Bref,配置serverless.yml定义函数和事件,如HTTP端点和Artisan命令;3.执行serverlessdeploy命令即可完成部署,自动配置APIGateway并生成访问URL;4.针对Lambda限制,Bref提供解决

PHP的垃圾回收机制基于引用计数,但循环引用需靠周期性运行的循环垃圾回收器处理;1.引用计数在变量无引用时立即释放内存;2.循环引用导致内存无法自动释放,需依赖GC检测并清理;3.GC在“可能根”zval达阈值或手动调用gc_collect_cycles()时触发;4.长期运行的PHP应用应监控gc_status()、适时调用gc_collect_cycles()以避免内存泄漏;5.最佳实践包括避免循环引用、使用gc_disable()优化性能关键区及通过ORM的clear()方法解引用对象,最

usearestapitobridgephpandmlmodelsbyrunningthemodelinpythonviaflaskorfastapiandcallingitfromphpusingcurlorguzzle.2.runpythonscriptsdirectsdirectlyectlyectlyfromphpsingexec()orshell_exec()orshell_exec()orshell_exec()
