0

我目前有一个功能齐全的脚本,可以将图片从我的服务器直接上传到我的 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);

我想将这两个工作脚本合二为一。我确信它很简单,所有代码都在这里,我只是不知道如何将一个进程链接到另一个进程。这是我能解释的最好的了..谢谢!

4

1 回答 1

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);
                }

公共函数 watermarkimages($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();



}
于 2016-08-25T04:24:31.990 回答