[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bulk-resize images, but retain IPTC data

Posted on 2006-03-27
3
Medium Priority
?
376 Views
Last Modified: 2008-07-03
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 />";
      }
}
?>
0
Comment
Question by:spikeyjames
1 Comment
 
LVL 2

Accepted Solution

by:
CaveyCoUk earned 440 total points
ID: 16321631
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question