使用GD将IPTC嵌入到PHP中的图像中

时间:2011-04-01 02:37:05

标签: php iptc

我正在尝试拍摄照片并创建缩略图而不会丢失包含版权信息和其他信息的IPTC信息。我正在使用GD进行调整大小的脚本,这当然会导致IPTC数据的丢失,因为它会创建一个完整的新文件,而不是实际调整原始文件的大小。所以,我的解决方案是从原始图像中提取IPTC数据,然后将其嵌入缩略图中。截至目前,一切运行正常,生成缩略图除了不复制IPTC数据。我的代码片段如下。谁能看到我失踪的任何人?这是基于iptcembed()的PHP手册中的示例。哦,是的,我在Zend框架内工作,但除了Registry以处理配置,这是非常简单的OOP代码。谢谢!

        public function resizeImage($image, $size)
    {
            // Get Registry
            $galleryConfig = Zend_Registry::get('gallery_config');

            $path = APPLICATION_PATH . $galleryConfig->paths->mediaPath . $image->path . '/';
            $file = $image->filename . '.' . $image->extension;
            $newFilename = $image->filename . '_' . $size . '.' . $image->extension;

            // Get Original Size
            list($width, $height) = getimagesize($path . $file);

            // Check orientation, create scalar
            if($width > $height){
                    $scale = $width / $size;
            }else{
                    $scale = $height / $size;
            }

            // Set Quality
            switch($size){
                    case ($size <= 200):
                            $quality = 60;
                            break;
                    case ($size > 200 && $size <= 600):
                            $quality = 80;
                            break;
                    case ($size > 600):
                            $quality = 100;
                            break;
            }

            // Recalculate new sizes with default ratio
            $new_width = round($width * (1 / $scale));
            $new_height = round($height * (1/ $scale));

            // Resize Original Image
            $imageResized = imagecreatetruecolor($new_width, $new_height);
            $imageTmp     = imagecreatefromjpeg ($path.$file);
            imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

            // Save File
            $complete = imagejpeg($imageResized, $path . $newFilename, $quality);

            // Copy IPTC info into new file
            $imagesize = getImageSize($path . $file, $info); 
            if(isset($info['APP13'])){ 
            $content = iptcembed($info['APP13'], $path . $newFilename); 
               $fw = fopen($path . $newFilename , 'wb'); 
               fwrite($fw, $content); 
               fclose($fw); 
            }

    }

1 个答案:

答案 0 :(得分:0)

我认为iptcembed会自动保存文件,这意味着php.net手册示例错误:

  

返回值   如果success和spool标志低于2,那么JPEG将不会作为字符串返回,错误时返回FALSE。

请参阅http://php.net/iptcembed

相关问题