我目前有一个功能齐全的脚本,可以将图片从我的服务器直接上传到我的 s3 存储桶......
但是,我现在想开始为每张上传的图像添加水印。如您所知,上传到 S3 从来没有真正将它存储在服务器上,就像一个临时文件一样。
基本上,我正在尝试将图像上传到 S3(已经有效),但在此之前,我想为该图像添加水印(无效)。
这是将文件上传到我的 S3 存储桶的工作代码:
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'BLAHBLAH');
if (!defined('awsSecretKey')) define('awsSecretKey', 'BLAHBLAH');
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
$fileName = $_FILES['theFile']['name'];
$fileTempName = $_FILES['theFile']['tmp_name'];
$fileSize = $_FILES['theFile']['size'];
$fileExt = substr($fileName, strrpos($fileName, '.') + 1);
$fileExt = strtolower($fileExt);
$custompostid = rand(1000000,9999999);
$imageName = $custompostid.".".$fileExt."";
//move the file
if ($s3->putObjectFile($fileTempName, "BUCKETNAME", $image, S3::ACL_PUBLIC_READ, array(), $imageType)) {
//success
}
else{
//error
}
但在上传文件之前,我想为每张图片添加水印。使用此功能:
// getting the image name from GET variable
$image = $_GET['image'];
// creating png image of watermark
$watermark = imagecreatefrompng('watermark.png');
// getting dimensions of watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// creting jpg from original image
$image_path = '/path/to/image/folder/' . $image;
$image = imagecreatefromjpeg($image_path);
//something went wrong
if ($image === false) {
return false;
}
// getting the dimensions of original image
$size = getimagesize($image_path);
// placing the watermark 5px from bottom and right
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
// blending the images together
imagealphablending($image, true);
imagealphablending($watermark, true);
// creating the new image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
// destroying and freeing memory
imagedestroy($image);
imagedestroy($watermark);
我想将这两个工作脚本合二为一。我确信它很简单,所有代码都在这里,我只是不知道如何将一个进程链接到另一个进程。这是我能解释的最好的了..谢谢!