2013-03-08 162 views
0

我现在有一些直接上传图片到从我的服务器我的S3存储一个全功能的脚本...PHP亚马逊S3与水印上传

不过,我想现在开始添加水印到每一个上传图片。如你所知,上传到S3永远不会真正将它存储在服务器上,就像临时文件一样。

基本上,我想上传一个图像到S3(已经工作),但之前,我想为该图像添加一个水印(不工作)。

这里是工作的代码THATS文件上载TO MY 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); 

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

+1

有没有回答的问题,或者至少一个问题你可以描述你写的代码吗? – 2013-03-08 17:42:25

+0

基本上,我想上传一个图像到S3(已经工作),但之前,我想为该图像添加水印(不工作)。 – Chrisl446 2013-03-08 17:50:22

+0

您需要更具体地说明什么是不工作而不是“不工作”。 – 2013-03-08 17:53:28

回答

0

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



}