Php照片修改

时间:2016-11-29 19:30:24

标签: php image png jpeg

您好我有一个包含3000张照片的文件夹,我需要一个脚本添加我的网站徽标但是图像,我有编辑图像的脚本,但我需要一个打开图像并制作过程的脚本,

我的剧本

<?php
        $logo = imagecreatefrompng("logo.png");

        header('Content-type: image/jpg');

        $image = imagecreatefromjpeg("image.jpg");

        imagecopy($image, $logo, 132, 95, 0, 0, 25, 25);

        imagejpeg($image);

        imagedestroy($image);
?>

如果有人有Windows程序请发给我。

1 个答案:

答案 0 :(得分:0)

我会这样做:

function add_png_logo(){
    $dir = APPPATH.'assets/img/'; # Where your PNG photos are
    $dirUpload = APPPATH.'assets/uploads/'; # Where they will be saved
    $baseUrl = 'http://127.0.0.1/tests/assets/uploads/'; # Base to link merged images
    # Where the work begins
    $iteractor = new DirectoryIterator($dir);
    $ext = ['png'];
    $i = 0;
    # Function to save individual files
    function save_png($getFilename,$getFilePath){
        $medida = array('width'=>'1024','height'=>'1024',);
        // Creates a temp image
        $TempPngFile = imagecreatetruecolor($medida['width'], $medida['height']);
        # Defines a transparent color to fill all image
        $TransparentColor = imagecolorallocatealpha($TempPngFile, 0, 0, 0, 127);
        imagefill($TempPngFile, 0, 0, $TransparentColor);
        # Forces transparency definition
        imagealphablending($TempPngFile, true);
        imagesavealpha($TempPngFile, true);
        # Open image
        $logo = imageCreateFromPng(APPPATH.'cache/mark.png');
        # Fix transparency definitions
        imageAlphaBlending($logo, true);
        imageSaveAlpha($logo, true);            
        # Open image
        $img2 = imageCreateFromPng($getFilename);
        # Forces transparency definition
        imageAlphaBlending($img2, true);
        imageSaveAlpha($img2, true);
        # insert $logo and $img2 in $TempPngFile and setting positioning
        imagecopy($TempPngFile, $img2, 0, 0, 0, 0, imagesx($img2), imagesy($img2));
        imagecopy($TempPngFile, $logo, 25, 25, 0, 0, imagesx($logo), imagesy($logo)); 
        # Save final image to $getFilePath
        imagepng($TempPngFile, $getFilePath);
        // Destroy images
        imageDestroy($TempPngFile);
        imageDestroy($logo);
        imageDestroy($img2);            
    }
    # Loop in $dir, get all PNG files to overlap $logo on left top
    foreach ($iteractor as $entry) {
        if ($entry->isFile()) {
            if (in_array($entry->getExtension(), $ext)) {
                $getFilename = $dir.$entry->getFilename();
                $getImageName = $entry->getFilename().'_'.$i++.'_.png';
                $getFilePath = $dirUpload.$getImageName;
                save_png($getFilename, $getFilePath);
                echo 'Created image: <a href="'.$baseUrl.$getImageName.'" target="_blank">'.$getImageName.'</a><br>';
            }
        }
    }
}

OBS:它使用php-gd扩展名。并且肯定有一种方法可以在重叠文件之前将JPG转换为PNG(或者你应该首先将你的3000张照片转换为PNG),但我现在太懒了,而且它有效!现在它在你的手上!