Jump to content

Editing a Gallery app....


mrheff

Recommended Posts

i have a form  i am currently using to allow for an upload and watermark of an image,

I want to include a batch feature which has been going well so far

 

Id like to use exactly the same process in my form but i cant use it because it uses the $file_[upload] i dont know how to change this part so it opens all my files, instead of the .zip,

the watermark from a single file script is...

<?php

    ob_start();

 

    $disp_width_max=150;                    // used when displaying watermark choices

    $disp_height_max=80;                    // used when displaying watermark choices

    $edgePadding=0;                        // used when placing the watermark near an edge

    $quality=100;                           // used when generating the final image

    $default_watermark='Sample-trans.png';  // the default image to use if no watermark was chosen

   

    if(isset($_POST['process'])){

        // an image has been posted, let's get to the nitty-gritty

        if(isset($_FILES['watermarkee']) && $_FILES['watermarkee']['error']==0){

       

            // be sure that the other options we need have some kind of value

            if(!isset($_POST['save_as'])) $_POST['save_as']='jpeg';

            if(!isset($_POST['v_position'])) $_POST['v_position']='bottom';

            if(!isset($_POST['h_position'])) $_POST['h_position']='right';

            if(!isset($_POST['wm_size'])) $_POST['wm_size']='1';

            if(!isset($_POST['watermark'])) $_POST['']=$default_watermark;

       

            // file upload success

            $size=getimagesize($_FILES['watermarkee']['tmp_name']);

            if($size[2]==2 || $size[2]==3){

                // it was a JPEG or PNG image, so we're OK so far

               

                $original=$_FILES['watermarkee']['tmp_name'];

                $target_name=date('YmdHis').'_'.

                    // if you change this regex, be sure to change it in generated-images.php:26

                    preg_replace('`[^a-z0-9-_.]`i','',$_FILES['watermarkee']['name']);

                $target=dirname(__FILE__).'/results/'.$target_name;

                $watermark=dirname(__FILE__).'/watermarks/'.$_POST['watermark'];

                $wmTarget=$watermark.'.tmp';

 

                $origInfo = getimagesize($original);

                $origWidth = $origInfo[0];

                $origHeight = $origInfo[1];

 

                $waterMarkInfo = getimagesize($watermark);

                $waterMarkWidth = $waterMarkInfo[0];

                $waterMarkHeight = $waterMarkInfo[1];

       

                // watermark sizing info

                if($_POST['wm_size']=='larger'){

                    $placementX=0;

                    $placementY=0;

                    $_POST['h_position']='center';

                    $_POST['v_position']='center';

                $waterMarkDestWidth=$waterMarkWidth;

                $waterMarkDestHeight=$waterMarkHeight;

                   

                    // both of the watermark dimensions need to be 5% more than the original image...

                    // adjust width first.

                    if($waterMarkWidth > $origWidth*1.05 && $waterMarkHeight > $origHeight*1.05){

                    // both are already larger than the original by at least 5%...

                    // we need to make the watermark *smaller* for this one.

                   

                    // where is the largest difference?

                    $wdiff=$waterMarkDestWidth - $origWidth;

                    $hdiff=$waterMarkDestHeight - $origHeight;

                    if($wdiff > $hdiff){

                    // the width has the largest difference - get percentage

                    $sizer=($wdiff/$waterMarkDestWidth)-0.05;

                    }else{

                    $sizer=($hdiff/$waterMarkDestHeight)-0.05;

                    }

                    $waterMarkDestWidth-=$waterMarkDestWidth * $sizer;

                    $waterMarkDestHeight-=$waterMarkDestHeight * $sizer;

                    }else{

                    // the watermark will need to be enlarged for this one

                   

                    // where is the largest difference?

                    $wdiff=$origWidth - $waterMarkDestWidth;

                    $hdiff=$origHeight - $waterMarkDestHeight;

                    if($wdiff > $hdiff){

                    // the width has the largest difference - get percentage

                    $sizer=($wdiff/$waterMarkDestWidth)+0.05;

                    }else{

                    $sizer=($hdiff/$waterMarkDestHeight)+0.05;

                    }

                    $waterMarkDestWidth+=$waterMarkDestWidth * $sizer;

                    $waterMarkDestHeight+=$waterMarkDestHeight * $sizer;

                    }

                }else{

                $waterMarkDestWidth=round($origWidth * floatval($_POST['wm_size']));

                $waterMarkDestHeight=round($origHeight * floatval($_POST['wm_size']));

                if($_POST['wm_size']==1){

                    $waterMarkDestWidth-=2*$edgePadding;

                    $waterMarkDestHeight-=2*$edgePadding;

                }

                }

 

                // OK, we have what size we want the watermark to be, time to scale the watermark image

                resize_png_image($watermark,$waterMarkDestWidth,$waterMarkDestHeight,$wmTarget);

               

                // get the size info for this watermark.

                $wmInfo=getimagesize($wmTarget);

                $waterMarkDestWidth=$wmInfo[0];

                $waterMarkDestHeight=$wmInfo[1];

 

                $differenceX = $origWidth - $waterMarkDestWidth;

                $differenceY = $origHeight - $waterMarkDestHeight;

 

                // where to place the watermark?

                switch($_POST['h_position']){

                    // find the X coord for placement

                    case 'left':

                        $placementX = $edgePadding;

                        break;

                    case 'center':

                        $placementX =  round($differenceX / 2);

                        break;

                    case 'right':

                        $placementX = $origWidth - $waterMarkDestWidth - $edgePadding;

                        break;

                }

 

                switch($_POST['v_position']){

                    // find the Y coord for placement

                    case 'top':

                        $placementY = $edgePadding;

                        break;

                    case 'center':

                        $placementY =  round($differenceY / 2);

                        break;

                    case 'bottom':

                        $placementY = $origHeight - $waterMarkDestHeight - $edgePadding;

                        break;

                }

       

                if($size[2]==3)

                    $resultImage = imagecreatefrompng($original);

                else

                    $resultImage = imagecreatefromjpeg($original);

                imagealphablending($resultImage, TRUE);

       

                $finalWaterMarkImage = imagecreatefrompng($wmTarget);

                $finalWaterMarkWidth = imagesx($finalWaterMarkImage);

                $finalWaterMarkHeight = imagesy($finalWaterMarkImage);

       

                imagecopy($resultImage,

                          $finalWaterMarkImage,

                          $placementX,

                          $placementY,

                          0,

                          0,

                          $finalWaterMarkWidth,

                          $finalWaterMarkHeight

                );

               

                if($size[2]==3){

                    imagealphablending($resultImage,FALSE);

                    imagesavealpha($resultImage,TRUE);

                    imagepng($resultImage,$target,$quality);

                }else{

                    imagejpeg($resultImage,$target,$quality);

                }

 

                imagedestroy($resultImage);

                imagedestroy($finalWaterMarkImage);

 

                // display resulting image for download

?>

   <div>

    <h1>Watermarked Image</h1>

    <p>

     <a href="<?php echo $_SERVER['PHP_SELF'] ?>">Back To Form</a>

    </p>

    <p>

     Below is the resulting watermarked image based on your input on the submission form. To save the image, right-click

     (control click on a Mac) and select the option similar to "Save Image As..." You can then save the image to your harddrive

     when prompted for a save location.

    </p>

    <p>

     Watermarked images are also saved on this system. You can view and/or delete watermarked images

     <a href="generated-images.php">here</a>.

    </p>

    <p align="center">

     <img src="results/<?php echo $target_name ?>?id=<?php echo md5(time()) ?>" alt="" />

    </p>

    <hr>

   </div>

<?php

                unlink($wmTarget);

            }else{

?>

   <div>

    <h1>Watermarked Image</h1>

    <p class="errorMsg">The image uploaded was not a JPEG or PNG image.</p>

   </div>

<?php

            }

        }else{

?>

   <div>

    <h1>Watermarked Image</h1>

    <p class="errorMsg">Unable to upload the image to watermark.</p>

   </div>

<?php

        }

    }else{

        // no image posted, show form for options

?>

    <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>">

     <table cellpadding="2" cellspacing="0" border="0">

      <tr>

       <th width="50%">

        Image to WaterMark:

       </th>

       <td width="50%">

        <input type="file" name="watermarkee" />

       </td>

      </tr>

      <tr>

       <th valign="top">

        Choose Watermark:<br />

        <p>

         watermarks placed in directory.

        </p>

       

       </th>

       <td valign="top">

        <table cellpadding="2" cellspacing="0" border="0">

<?php

        $dir=dirname(__FILE__).'/watermarks';

        if (is_dir($dir)) {

            if ($dh = opendir($dir)) {

                $i=0;

                $watermarks=array();

                while (($file = readdir($dh)) !== FALSE) {

                    if(!preg_match('`\.png$`',$file)) continue;

                    $watermarks[]=$file;

                }

                closedir($dh);

               

                // now sort the array according to file name

                asort($watermarks);

               

                foreach($watermarks as $file){

                    // restrain display width

                    $size=getimagesize($dir.'/'.$file);

                    if($size[0] > $disp_width_max && $disp_width_max){

                        $reduction=$disp_width_max / $size[0];

                        $size[0]=$size[0]*$reduction;

                        $size[1]=$size[1]*$reduction;

                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);

                       

                        // also restrain the height after a resize

                        if($size[1] > $disp_height_max && $disp_height_max){

                            $reduction=$disp_height_max / $size[1];

                            $size[0]=$size[0]*$reduction;

                            $size[1]=$size[1]*$reduction;

                            $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);

                        }

                    }

 

                    // also restrain the height

                    if($size[1] > $disp_height_max && $disp_height_max){

                        $reduction=$disp_height_max / $size[1];

                        $size[0]=$size[0]*$reduction;

                        $size[1]=$size[1]*$reduction;

                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);

                    }

 

                    echo '      <tr>',"\n",

                         '       <td>',"\n",

                         '        <input type="radio" value="',$file,'" name="watermark" /> ',

                         '<img src="watermarks/',$file,'" ',$size[3],' alt="" />',"\n",

                         '       </td>',"\n",

                         '      </tr>',"\n";

 

                    $i++;

                }

                if($i==0){

                    // no images at this time

                    echo '     <tr>',"\n",

                         '      <td>',"\n",

                         '       There are currently no watermark images to use with this the system on this server.',"\n",

                         '      </td>',"\n",

                         '     </tr>',"\n";

                }

            }

        }

?>

        </table>

       </td>

      </tr>

        <tr>

       <td align="center" colspan="2">

        <input type="submit" name="process" value="Watermark Image" />

       </td>

      </tr>

     </table>

    </form>

   </div>

<?php

    }

    $page_display=ob_get_clean();

 

function resize_png_image($img,$newWidth,$newHeight,$target){

    $srcImage=imagecreatefrompng($img);

    if($srcImage==''){

        return FALSE;

    }

    $srcWidth=imagesx($srcImage);

    $srcHeight=imagesy($srcImage);

    $percentage=(double)$newWidth/$srcWidth;

    $destHeight=round($srcHeight*$percentage)+1;

    $destWidth=round($srcWidth*$percentage)+1;

    if($destHeight > $newHeight){

        // if the width produces a height bigger than we want, calculate based on height

        $percentage=(double)$newHeight/$srcHeight;

        $destHeight=round($srcHeight*$percentage)+1;

        $destWidth=round($srcWidth*$percentage)+1;

    }

    $destImage=imagecreatetruecolor($destWidth-1,$destHeight-1);

    if(!imagealphablending($destImage,FALSE)){

        return FALSE;

    }

    if(!imagesavealpha($destImage,TRUE)){

        return FALSE;

    }

    if(!imagecopyresampled($destImage,$srcImage,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight)){

        return FALSE;

    }

    if(!imagepng($destImage,$target)){

        return FALSE;

    }

    imagedestroy($destImage);

    imagedestroy($srcImage);

    return TRUE;

}

 

echo '<?xml version="1.0" encoding="iso-8859-1"?>',"\n"; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

  <title>Watermarking JPEG and PNG Images with PHP and GD2</title>

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

  <meta name="description" content="A PHP implementation to watermark JPEG or PNG images with a PNG-24 image with alpha transparency." />

  <style type="text/css">

       th{

        text-align: right;

        font-weight: bold;

    }

    th p{

        font-weight: normal;

        font-size: 75%;

        color: #c00;

        background-color: transparent;

    }

  </style>

 

</head>

<body>

    <div>

    <?php echo $page_display ?></div>

<div><h1><a href="batch.php">Batch Upload</a></h1> 

 

</div>

</body>

</html>

 

 

 

 

 

 

 

But i want to change it so it batch process all of the zip file contents i have an unzip script which looks like this..

this unzip also shows the contents of the unzip when decompressed anyone know how i can rid myself of the zip file too?

im guessiing unlink() but i dont quite know where to stick it.

 

 

 

 

<?php 

  //Define some variables 

      $dir = "batch/"; //B&#7841;n nên thay ð&#7893;i ðý&#7901;ng d&#7851;n cho phù h&#7907;p 

    //Ki&#7873;u file, Gif, jpeg, zip ::b&#7841;n có th&#7875; s&#7917;a ð&#7893;i n&#7871;u thích 

      $types = array("image/gif","image/pjpeg","application/x-zip-compressed"); 

     

//Check to determine if the submit button has been pressed 

    if(isset($_POST['submit'])){ 

 

//Shorten Variables 

     $tmp_name = $_FILES['upload']['tmp_name']; 

     $new_name = "batch.zip"; 

 

//Check MIME Type 

    if (in_array($_FILES['upload']['type'], $types)){ 

                     

         //Move file from tmp dir to new location 

 

        move_uploaded_file($tmp_name,$dir . $new_name); 

           

        echo "{$_FILES['upload']['name']} was uploaded sucessfully!, and unzipped processing";

 

 

 

 

                                   

           

    }else{ 

                 

    //Print Error Message 

 

     echo "<small>File <strong><em>{$_FILES['upload']['name']}</em></strong> Was Not Uploaded!</small><br />"; 

       

    //Debug 

   $name =  $_FILES['upload']['name']; 

   $type =    $_FILES['upload']['type']; 

   $size =    $_FILES['upload']['size']; 

   $tmp =     $_FILES['upload']['name']; 

     

   echo "Name: $name<br/ >Type: $type<br />Size: $size<br />Tmp: $tmp"; 

               

    } 

       

    }       

       

else{       

       

    echo 'to upload a zip file please use the small form below'; 

 

       

        ?>

 

 

<?php

    $dir=dirname(__FILE__).'/batch';

    if (is_dir($dir)) {

        if ($dh = opendir($dir)) {

            $i=0;

            echo '    <div id=gallery><table cellpadding="3" cellspacing="0" border="0">',"\n";

            $watermarks=array();

            while (($file = readdir($dh)) !== FALSE) {

                if($file=='.' || $file=='..') continue;

                $watermarks[]=$file;

            }

            closedir($dh);

           

            // now sort the array according to file name

            asort($watermarks);

           

            foreach($watermarks as $file){

                // restrain display width

                $size=getimagesize($dir.'/'.$file);

                if($size[0] > $disp_width_max && $disp_width_max){

                    $reduction=$disp_width_max / $size[0];

                    $size[0]=$size[0]*$reduction;

                    $size[1]=$size[1]*$reduction;

                    $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);

                   

                    // also restrain the height after a resize

                    if($size[1] > $disp_height_max && $disp_height_max){

                        $reduction=$disp_height_max / $size[1];

                        $size[0]=$size[0]*$reduction;

                        $size[1]=$size[1]*$reduction;

                        $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);

                    }

                }

 

                // also restrain the height

                if($size[1] > $disp_height_max && $disp_height_max){

                    $reduction=$disp_height_max / $size[1];

                    $size[0]=$size[0]*$reduction;

                    $size[1]=$size[1]*$reduction;

                    $size[3]=sprintf('width="%d" height="%d"',$size[0],$size[1]);

                }

 

                echo

                     '      <td>',"\n",

                     '       <a href="batch/',$file,'" target="_blank"> <img src="show_batch.php?filename=batch/',$file,'&height=100" border="0"','/> </a>',"\n",

                     '      </td>',"\n";

 

                     

                $i++;

            }

            if($i==0){

                // no images at this time

                echo '     <tr>',"\n",

                     '      <td>',"\n",

                     '       There are currently no images saved in the results folder.',"\n",

                     '      </td>',"\n",

                     '     </tr>',"\n";

            }

            echo '    </table></div>',"\n",

                 '   </div>',"\n\n";

        }

    }

    $page_display=ob_get_clean();

?>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

  <title>Batch processing</title>

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>

<body>

 

           

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> 

       

      <fieldset> 

              <legend>Upload Files</legend> 

                                           

          <input type="file" name="upload" /> 

</fieldset> 

<input type="submit" name="submit" value="Upload Files" /> 

</form>

<style type="text/css">

body {background:#000000;

color:#ffffff;}

#gallery{

color:#ffffff;

bottom:0px;

align:center;

margin:auto;

width:100%;

height:175px;

overflow-x:auto; 

background:#000000;

text-align:center;

scrollbar-face-color: #000000;

scrollbar-shadow-color: #ffffff;

scrollbar-highlight-color: #ffffff;

scrollbar-3dlight-color: #000000;

scrollbar-darkshadow-color: #000000;

scrollbar-track-color: #000000;

scrollbar-arrow-color: #FFFFFF;}

#gallery table td{vertical-align:bottom;}

table td{vertical-align:bottom;}

</style> 

   

   <div id=gallery>

    <?php echo $page_display ?>

</div>

</body>

</html>

 

youll have to excusse the thumbnail class but you get th idea...

 

 

 

Does anyone have any ideas? thanks for your time even if you just read this.

Yours, a square eyed newbie despreately seeking an answer.

Link to comment
https://forums.phpfreaks.com/topic/40535-editing-a-gallery-app/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.