我目前有一个FULLY FUNCTIONING脚本,可以将图片从我的服务器直接上传到我的s3存储桶......
但是,我现在想开始为每个上传的图像添加水印。正如您所知,上传到S3从未真正将其存储在服务器上,就像临时文件一样。
基本上,我正在尝试将图像上传到S3(已经有效),但在此之前,我想为该图像添加水印(不工作)。
这是上传文件到我的S3 BUCKET的工作代码:
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);
我想将这两个工作脚本合二为一。我确信它很简单,所有代码都在这里,我只是不知道如何将一个进程链接到另一个进程。这是我能解释的最好的结果..谢谢!
答案 0 :(得分:0)
使用Watermark Codeigniter上传S3图像
$s3_bucket_name = $this->config->item('s3_bucket_name');
$s3_access_key = $this->config->item('s3_access_key');
$s3_secret_key = $this->config->item('s3_secret_key');
include('amazon/s3_config.php');
$uploaddir = "images/";
$actual_image_name = time().'.'.$ext;
$newname=$uploaddir.$actual_image_name;
if (move_uploaded_file($_FILES['photos']['tmp_name'], $newname))
{
$this->watermarkimages($uploaddir,$actual_image_name);
$result_image='/'.$newname;
if($s3->putObjectFile($newname, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) )
{
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
mysql_query("INSERT INTO photos(product_image,product_id) VALUES('$s3file','$prd_id')");
}
unlink($newname);
}
公共功能水印图像($ uploaddir,$ image_name) {
$masterURL =$uploaddir.$image_name;
$path='images/watermark3.png';
$this->load->library('image_lib');
$config1['image_library'] = 'gd2';
$config1['source_image'] = $masterURL;
$config1['wm_type'] = 'overlay';
$config1['wm_overlay_path'] = $path;
$config1['wm_opacity'] = 100;
$config1['wm_vrt_alignment'] = 'top';
$config1['wm_hor_alignment'] = 'right';
$this->image_lib->initialize($config1);
$this->image_lib->watermark();
}