在PHP上载图像中心图像

在PHP上载图像中心图像

本文介绍了在PHP上载图像中心图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让用户上传始终方形图像(可变尺寸,但总是正方形),我想在正方形的中间覆盖我自己的图像(也就是说,正方形和固定尺寸)图像,无论大小。

I have users uploading always-square images (of variable dimensions, but always square), and I would like to overlay my own image (also, as it was, a square and of fixed dimensions) in the middle of the square image, regardless of the size.

我知道我可以使用GD并使用尺寸,但有没有办法用更少的代码行自动居中?

I know I could use GD and play with the dimensions, but is there an way to auto-center it with fewer lines of code?

我不需要任何花哨的东西,只需将图像放入另一张图像的中心。

I don't need anything fancy, just an image put into the center of another image.

推荐答案

我很困惑你是否在GD中询问如何做到这一点,或者是GD的替代品。如果您想使用GD库,可以使用 imagecopy()函数并尝试以下内容:

I'm confused whether you're asking how to do this in GD, or an alternative to GD. If you wanted to use the GD library, you could use the imagecopy() function and attempt something like the following:

<?php
$uploaded = imagecreatefrompng("../image.png"); //user uploaded
$watermark = imagecreatefrompng("../logo.png"); //watermark or logo

$x = imagesx($uploaded) / 2 - imagesx($watermark) / 2;
$y = imagesy($uploaded) / 2 - imagesy($watermark) / 2;

imagecopy($uploaded, $watermark, $x, $y, 0, 0, imagesx($watermark), imagesy($watermark));

header("Content-type: image/png");
imagepng($uploaded);

imagedestroy($uploaded);
imagedestroy($watermark);

否则,您能否更具体地了解您的需求?我的意思是,你可以使用像ImageMagick这样的不同库来实现它,但不一定有更好的方法。

Otherwise, could you be a bit more specific on what you want? I mean, you could do it with different libraries like ImageMagick, but there aren't necessarily better ways to do it.

这篇关于在PHP上载图像中心图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:00