Bulk-resize images, but retain IPTC data

I've written a script to batch-resize images in a directory, but when the code is executed the newly resized images have lost their IPTC data. It is important to retain this data - but how?

The code is below:

-----------------------------------------------------------------------------------------------------------------

<?php
set_time_limit(0);
$filepath = "./";
$dir = dir($filepath);
while($entry=$dir->read()) {
      if($entry == "." || $entry == "..") {
            continue;
      }
      $filename = $entry;
      $fp = @fopen("$filepath/$filename","r");
      $image_stats = getimagesize($filepath."/".$filename);
      $imagewidth = $image_stats[0];
      $imageheight = $image_stats[1];
      $img_type = $image_stats[2];
      if(imagewidth !== 500){
            if ($imagewidth < 500){
                  $new_w = $imagewidth;
            } else {
                  $new_w = 500;
            }
            $ratio = ($imagewidth / $new_w);
            $new_h = round($imageheight / $ratio);
            if ($img_type=="2") {
                  $src_img = imagecreatefromjpeg($filepath."/".$filename);
                  $dst_img = imagecreatetruecolor($new_w,$new_h);
                  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                  imagejpeg($dst_img, $filepath."/".$filename);
            } elseif ($img_type=="3") {
                  $dst_img=imagecreate($new_w,$new_h);
                  $src_img=imagecreatefrompng($cfg_fullsizepics_path."/".$entry);
                  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                  imagepng($dst_img, $filepath."/".$filename);
            } elseif  ($img_type=="1") {
                  $src_img = imagecreatefromgif($cfg_fullsizepics_path."/".$entry);
                  $dst_img = imagecreatetruecolor($new_w,$new_h);
                  imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                  imagegif($dst_img, $filepath."/".$filename);
            }
            echo "Resized: ".$filename."<br />";
      }
}
?>
LVL 1
spikeyjamesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CaveyCoUkCommented:
What a convoluted function iptcembed is. I had figured out the functions I needed to use, but then it still was not working and I couldnt understand why.  

It just happens that I was using iptcembed wrong (Doh!) and the hint is in the bottom post on http://www.php.net/iptcembed

It turns out that iptcembed does not embed the IPTC code into the file, it combines the IPTC code in the file and returns it as a variable so that you can then output it.  

Anyway, this works -

<?php
set_time_limit(0);

/* Input directory */
$filepath = "./images";
/* Output Directory */
$outputpath = "./images_out";

$wanted_width=500;

$dir = opendir($filepath);

while($entry=readdir($dir)) {
      if ( is_file($filepath."/".$entry) && exif_imagetype($filepath."/".$entry) ){
            /* Only if its an image */
            $filename = $filepath."/".$entry;
            $outputname = $outputpath."/".$entry;
            
            echo "Working on ".$filename."<br />";
            
            /* The change here gets a pile of info into $image_info - this may be used later if it is a JPEG */
            $image_stats = getimagesize($filename,$image_info);
            $imagewidth = $image_stats[0];
            $imageheight = $image_stats[1];
            $img_type = $image_stats[2];
            
            if(imagewidth !== $wanted_width){
                  if ($imagewidth < $wanted_width){
                        $new_w = $imagewidth;
                  } else {
                        $new_w = $wanted_width;
                  }
                  $ratio = ($imagewidth / $new_w);
                  $new_h = round($imageheight / $ratio);
                  $dst_img = imagecreatetruecolor($new_w,$new_h);
                  if ($img_type=="2") {
                        $src_img = imagecreatefromjpeg($filename);
                        imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                        imagejpeg($dst_img, $outputname);
                        /* First check if it has IPTC info (stored in $image_info["APP13"] which we got earlier) */
                        if ($iptcinfo = ((isset($image_info["APP13"]))?$image_info["APP13"]:false)){
                              /* As it does, we embed it into the new file */
                              echo "&nbsp;Embedding IPTC into ".$outputname."<br />";
                              /*  If you want to see the IPTC info, include the following lines
                                    echo "<textarea style='width:500px;height:250px'>";
                                    print_r(iptcparse($image_info["APP13"]));
                                    echo "</textarea><br />";
                              */
                              $contents = iptcembed ($iptcinfo,$outputname,0);
                              $fp = fopen($outputname, "w+b");
                              fwrite($fp, $contents);
                              fclose($fp);
                        }
                  } elseif ($img_type=="3") {
                        $src_img=imagecreatefrompng($cfg_fullsizepics_path."/".$entry);
                        imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                        imagepng($dst_img, $outputname);
                  } elseif  ($img_type=="1") {
                        $src_img = imagecreatefromgif($cfg_fullsizepics_path."/".$entry);
                        imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
                        imagegif($dst_img, $outputname);
                  }
                  echo "&nbsp;Resized: ".$filename."<br />";
            }
      }
}
?>

Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.