簡體   English   中英

PHP中的作物和水印圖像

[英]crop and watermark image in php

我正在嘗試添加水印圖像以供用戶輸入。 它必須將處理后的圖像存儲在一個文件夾中。這是我用來上傳圖像和添加水印的代碼。

水印正在應用。 但是空圖像存儲在服務器中。 你能幫我做這個工作嗎? 這是我到目前為止所做工作

<?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


?>

嘗試這個

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>

此功能將獲取圖像的鏈接,並將水印放置在圖像的右下角。 假設水印為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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM