Jump to content

very new to php... trying to loop an image processing function


NTIV10

Recommended Posts

I have read so many articles about loops and processing images, etc. I finally have the code the way I want it for the images. The only thing is that now I need to process all of the files in a directory by running each one through the function. I am sure it's probably not that hard but I can't wrap my mind around it.

 

the code is sort of jacked up because imagick isn't enabled on my server so I have to exec.

 

<?php

$resource = $_GET["image"];
$im = imagecreatefrompng("wp-content/uploads/2009/11/{$resource}.png");
$width = imagesx( $im );
$height = imagesy( $im );

function thumb($im,$calc_width,$calc_height,$resource)
{	  
imagepng($im, "images/temp/prep.png");
  $step3="/usr/bin/convert -thumbnail 255x170 -gravity center -crop 215x143+0+0 -limit memory 32 -limit map 64 images/temp/prep.png images/temp/temp.png";

  exec ($step3);
    imagedestroy($im);
  $final=imagecreatefrompng("images/temp/temp.png");
imagepng($final,"images/{$resource}_thumb.png");

			echo "Thumb OK <br /><br /><img src=\"images/{$resource}_thumb.png\">";
			}
  thumb($im,$calc_width,$calc_height,$resource);

$im2 = imagecreatefrompng("wp-content/uploads/2009/11/{$resource}.png");
$width2 = imagesx( $im2 );
$height2 = imagesy( $im2 );
$textsize = $width2 / 13;

imagepng($im2, "images/temp/prep.png");

  $step1="/usr/bin/convert -size {$width}x{$height} xc:white /images/temp/watermark.png";
  $step2="/usr/bin/convert -size {$width}x{$height} -gravity center xc:black -fill \"#666666\" -font \"Helvetica\" -pointsize $textsize -annotate 45x45 \"© all rights reserved\" -fill black -annotate 45x45-0-1 \"© all rights reserved\" images/temp/watermarkmask.png";
  $step3="/usr/bin/convert -gravity center -limit memory 32 -limit map 64 -composite images/temp/prep.png images/temp/watermark.png images/temp/watermarkmask.png images/temp/wmtemp.png";
  exec ($step1);
  exec ($step2);
  exec ($step3);
  $watermarked=@imagecreatefrompng("images/temp/wmtemp.png");
imagepng($watermarked,"images/{$resource}web.png");

			echo "<br /><br />Watermarked OK <br /><br /><img src=\"images/{$resource}web.png\">";

$image = $watermarked;
$new_width = 900;
$new_height = 600;

$width = imagesx( $image );
$height = imagesy( $image );

function resample_image($width,$height,$new_width,$new_height,$image,$resource)
{
if( $width <= $new_width)
	{
		imagepng( $im , "images/{$resource}_noresize.png" );
			echo "<br /><br />No resize! <br /><br /><img src=\"images/{$resource}_noresize.png\">";
		}
	else
	{
		if( $width >= $height )
		{
			$calc_width = $new_width;
			$calc_height = floor( $height * ( $new_width / $width ) );
			$temp = imagecreatetruecolor( $calc_width, $calc_height );
			imagecopyresampled( $temp, $image,0,0,0,0,$calc_width,$calc_height,$width,$height );

			imagepng( $temp,"images/{$resource}_{$calc_width}.png" );
			echo "<br /><br />Resized OK <br /><br /><img src=\"images/{$resource}_{$calc_width}.png\">";
			}
		else
		{
			$calc_height = $new_height;
			$calc_width = floor( $width * ( $new_height / $height ) );

			$temp = imagecreatetruecolor( $calc_width, $calc_height );
			imagecopyresampled( $temp, $image,0,0,0,0,$calc_width,$calc_height,$width,$height );
			imagepng( $temp,"images/{$resource}_{$calc_width}.png" );
			echo "<br /><br />Resized OK <br /><br /><img src=\"images/{$resource}_{$calc_width}.png\">";
			}

	  }

}

resample_image($width,$height,$new_width,$new_height,$image,$resource);
echo "<br /><br />Done";

?>

 

The code works perfectly it does everything I need to do with my images, but only one at a time. So right now I go to create.php?image=imagenamehere.  How do I make it so I can enter in exactly the same url, but it will loop through the directory and apply all of my functions to each image, spitting out one long page of processed photos?

 

Thank you SO much! I am trying to teach myself how to do this and I was about to jump out of my chair when I actually got the images script to do what I wanted it to do. This is really the last step of my urgent PHP needs to get up and running... then I can learn some tweaks and cool stuff. I'm stuck with a non-starter until I can do this.

Well first, take all this

 

$width = imagesx( $im );
$height = imagesy( $im );

function thumb($im,$calc_width,$calc_height,$resource)
{     
imagepng($im, "images/temp/prep.png");
     $step3="/usr/bin/convert -thumbnail 255x170 -gravity center -crop 215x143+0+0 -limit memory 32 -limit map 64 images/temp/prep.png images/temp/temp.png";

     exec ($step3);
       imagedestroy($im);
     $final=imagecreatefrompng("images/temp/temp.png");
imagepng($final,"images/{$resource}_thumb.png");

            echo "Thumb OK <br /><br /><img src=\"images/{$resource}_thumb.png\">";
            }
     thumb($im,$calc_width,$calc_height,$resource);

$im2 = imagecreatefrompng("wp-content/uploads/2009/11/{$resource}.png");
$width2 = imagesx( $im2 );
$height2 = imagesy( $im2 );
$textsize = $width2 / 13;

imagepng($im2, "images/temp/prep.png");

     $step1="/usr/bin/convert -size {$width}x{$height} xc:white /images/temp/watermark.png";
     $step2="/usr/bin/convert -size {$width}x{$height} -gravity center xc:black -fill \"#666666\" -font \"Helvetica\" -pointsize $textsize -annotate 45x45 \"© all rights reserved\" -fill black -annotate 45x45-0-1 \"© all rights reserved\" images/temp/watermarkmask.png";
     $step3="/usr/bin/convert -gravity center -limit memory 32 -limit map 64 -composite images/temp/prep.png images/temp/watermark.png images/temp/watermarkmask.png images/temp/wmtemp.png";
     exec ($step1);
     exec ($step2);
     exec ($step3);
     $watermarked=@imagecreatefrompng("images/temp/wmtemp.png");
imagepng($watermarked,"images/{$resource}web.png");

            echo "<br /><br />Watermarked OK <br /><br /><img src=\"images/{$resource}web.png\">";

$image = $watermarked;
$new_width = 900;
$new_height = 600;

$width = imagesx( $image );
$height = imagesy( $image );

function resample_image($width,$height,$new_width,$new_height,$image,$resource)
{
   if( $width <= $new_width)
      {
         imagepng( $im , "images/{$resource}_noresize.png" );
            echo "<br /><br />No resize! <br /><br /><img src=\"images/{$resource}_noresize.png\">";
         }
      else
      {
         if( $width >= $height )
         {
            $calc_width = $new_width;
            $calc_height = floor( $height * ( $new_width / $width ) );
            $temp = imagecreatetruecolor( $calc_width, $calc_height );
            imagecopyresampled( $temp, $image,0,0,0,0,$calc_width,$calc_height,$width,$height );
            
            imagepng( $temp,"images/{$resource}_{$calc_width}.png" );
            echo "<br /><br />Resized OK <br /><br /><img src=\"images/{$resource}_{$calc_width}.png\">";
            }
         else
         {
            $calc_height = $new_height;
            $calc_width = floor( $width * ( $new_height / $height ) );
            
            $temp = imagecreatetruecolor( $calc_width, $calc_height );
            imagecopyresampled( $temp, $image,0,0,0,0,$calc_width,$calc_height,$width,$height );
            imagepng( $temp,"images/{$resource}_{$calc_width}.png" );
            echo "<br /><br />Resized OK <br /><br /><img src=\"images/{$resource}_{$calc_width}.png\">";
            }
            
        }

}

resample_image($width,$height,$new_width,$new_height,$image,$resource);
echo "<br /><br />Done";

 

And put it into a single function, called ProcessImage($imgname)

 

and then use this code

 

$dir = opendir("img directory");

//List files in images directory
while (($file = readdir($dir)) !== false)
  {
if(!is_file($file))
{
continue;
}
$F = basename($file);
ProcessImage($F);
  }

closedir($dir);

Cool! That worked after a little tweaking. Now, how do I keep it from spitting out 500 errors once it gets through about 10 images? I have it doing imagedestroy on each one and I tried the memory limit all over the place... does a 500 error mean out of memory or what?

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.