简体   繁体   中英

crop and watermark image in php

I am trying to add watermark image for user input. It must store processed image in a folder .Here is the code i used to upload the image and add watermark.

The watermark is applying. But empty image is storing in server. Can you please help me to make this work? here is the 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


?>

Try this

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

Upload form

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

This function will take an image's link and position the watermark at the right bottom of your image. assuming that the watermark is 200x20 pixels.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM