缩放水印以适合父图像 [英] Scaling a watermark to fit parent image

查看:179
本文介绍了缩放水印以适合父图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的照片尺寸各不相同,无论是横向,纵向还是正方形,我需要为每张照片制作最适合的水印 - 所以我需要调整水印的宽度(没有Imagick),因为它是长矩形,所以高度无所谓。

My photo sizes vary, they are either landscape, portrait or square, and I need to make a watermark the best fit for each photo - so I need to resize just the width of the watermark (without Imagick), as it's a long rectangle shape, so height doesn't matter.

我找到了PHP函数, imagecopyresized ,但是我老实说,即使在查看PHP文档之后,我也无法确定我的情况需要哪些参数!我也不确定在使用imagecopyresized后,我的其余功能将在获得水印宽度和高度的地方工作。

I found the PHP function, imagecopyresized, but I'll be honest, I can't work out what parameters are needed for my situation, even after looking at PHP documentation! I'm also not sure if after using imagecopyresized, the rest of my function will work where it gets the watermark width and height.

有人可以帮助我完成任务线。这是我得到了多远,它所需要的是添加到 imagecopyresized 部分的正确参数:

Can somebody help me get over the finish line. This is how far I got, all it needs is the right parameters added to the imagecopyresized part:

<?php

header('content-type: image/jpeg');

$image = imagecreatefromjpeg('https://.....jpg');
$imageSize = getimagesize('https://.....jpg');

$newWatermarkWidth = $imageSize[0]-50; // width of image minus 50px
$watermark = imagecreatefrompng('watermark.png');

// resize watermark to newWatermarkWidth here with imagecopyresize
$watermark = imagecopyresized(?,?,?,?);

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

$dest_x = ($imageSize[0]/2) - ($watermark_width/2) ;
$dest_y = ($imageSize[1]/2) - ($watermark_height/2);

imagecopy($image, $watermark, round($dest_x,0), round($dest_y,0), 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

?>






这就是我用& amp; ;完美的工作

一个脚本,用于调整水印的宽度以适合整个父图像,居中且成比例。

A script that adjusts the width of a watermark to fit across the whole parent image, centered and proportional.

<?php
header('content-type: image/jpeg');

$image = imagecreatefromjpeg('http://mydomain.com/myPhoto.jpg');
$imageSize = getimagesize('http://mydomain.com/myPhoto.jpg');

$watermark = imagecreatefrompng('http://mydomain.com/myWatermark.png');

$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);

$newWatermarkWidth = $imageSize[0]-20;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;

imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));

imagejpeg($image);

imagedestroy($image);
imagedestroy($watermark);


推荐答案

这会调整水印大小并直接复制到图像。

This resizes the watermark and copies directly to the image.

您不再需要现有的imagecopy专线。

You don't need the existing imagecopy line anymore.

$success = imagecopyresized($image,                 // Destination image
           $watermark,                              // Source image
           $imageSize[0]/2 - $newWatermarkWidth/2,  // Destination X
           $imageSize[1]/2 - imagesy($watermark)/2, // Destination Y
           0,                                       // Source X
           0,                                       // Source Y
           $newWatermarkWidth,                      // Destination W
           imagesy($watermark),                     // Destination H
           imagesx($watermark),                     // Source W
           imagesy($watermark));                    // Source H

这篇关于缩放水印以适合父图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆