在PHP中的裁剪和水印图像

时间:2016-11-05 10:32:30

标签: javascript php

我正在尝试为用户输入添加水印图像。它必须将处理过的图像存储在一个文件夹中。这是我用来上传图像和添加水印的代码。

水印正在应用。但空图像存储在服务器中。能帮我做这个工作吗?这是work I done till now

<?php 
    define('UPLOAD_DIR', 'images/');

    $img = $_POST['image-data'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);

    $file = UPLOAD_DIR . uniqid() . '.png';
     file_put_contents($file, $data);




$stamp = imagecreatefrompng('http://mygreetins.esy.es/new/images/watermark.png');
$im = imagecreatefrompng($file);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));





$file = UPLOAD_DIR . time() . '.png';
    $success = file_put_contents($file, $im);





echo $file


?>

2 个答案:

答案 0 :(得分:0)

试试这个

if(isset($_FILES['image_file']))
{
    $max_size = 800; //max image size in Pixels
    $destination_folder = 'D:/Websites/watermark/images';
    $watermark_png_file = 'watermark.png'; //watermark png file

    $image_name = $_FILES['image_file']['name']; //file name
    $image_size = $_FILES['image_file']['size']; //file size
    $image_temp = $_FILES['image_file']['tmp_name']; //file temp
    $image_type = $_FILES['image_file']['type']; //file type

    switch(strtolower($image_type)){ //determine uploaded image type 
            //Create new image from file
            case 'image/png': 
                $image_resource =  imagecreatefrompng($image_temp);
                break;
            case 'image/gif':
                $image_resource =  imagecreatefromgif($image_temp);
                break;          
            case 'image/jpeg': case 'image/pjpeg':
                $image_resource = imagecreatefromjpeg($image_temp);
                break;
            default:
                $image_resource = false;
        }

    if($image_resource){
        //Copy and resize part of an image with resampling
        list($img_width, $img_height) = getimagesize($image_temp);

        //Construct a proportional size of new image
        $image_scale        = min($max_size / $img_width, $max_size / $img_height); 
        $new_image_width    = ceil($image_scale * $img_width);
        $new_image_height   = ceil($image_scale * $img_height);
        $new_canvas         = imagecreatetruecolor($new_image_width , $new_image_height);

        if(imagecopyresampled($new_canvas, $image_resource , 0, 0, 0, 0, $new_image_width, $new_image_height, $img_width, $img_height))
        {

            if(!is_dir($destination_folder)){ 
                mkdir($destination_folder);//create dir if it doesn't exist
            }

            //center watermark
            $watermark_left = ($new_image_width/2)-(300/2); //watermark left
            $watermark_bottom = ($new_image_height/2)-(100/2); //watermark bottom

            $watermark = imagecreatefrompng($watermark_png_file); //watermark image
            imagecopy($new_canvas, $watermark, $watermark_left, $watermark_bottom, 0, 0, 300, 100); //merge image

            //output image direcly on the browser.
            header('Content-Type: image/jpeg');
            imagejpeg($new_canvas, NULL , 90);

            //Or Save image to the folder
            //imagejpeg($new_canvas, $destination_folder.'/'.$image_name , 90);

            //free up memory
            imagedestroy($new_canvas); 
            imagedestroy($image_resource);
            die();
        }
    }
}

上传表单

<form action="" id="upload-form" method="post" enctype="multipart/form-data">
<input type="file" name="image_file" />
<input type="submit" value="Send Image" />
</form>

答案 1 :(得分:0)

此功能将拍摄图像链接并将水印定位在图像的右下角。假设水印是200x20像素。

function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 90){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];
    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;
        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 90;
            break;
        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 90;
            break;
        default:
            return false;
            break;
    }
    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $watermark = imagecreatefrompng("images/watermark.png");
    $src_img = $image_create($source_file);
    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    $w_xpos=($max_width - 200);
    $w_ypos=($max_height - 20); 
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
        imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
        imagecopyresampled($dst_img , $watermark , $w_xpos , $w_ypos , 0 , 0 , 200 , 20 , 200 , 20 );
    }else{
        //cut point by width
        $w_point = (($width - $width_new) / 2);
        imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
        imagecopyresampled($dst_img , $watermark , $w_xpos , $w_ypos , 0 , 0 , 200 , 20 , 200 , 20 );
    }
    $image($dst_img, $dst_dir, $quality);
    if($watermark)imagedestroy($watermark);
    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);
}