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

发布于 2024-10-29 02:25:26 字数 2443 浏览 2 评论 0原文

我正在尝试拍摄照片并创建缩略图,而不会丢失包含版权信息和其他信息的 IPTC 信息。我使用 GD 编写脚本来调整大小,这当然会导致 IPTC 数据丢失,因为它创建了一个全新的新文件,而不是实际调整原始文件的大小。因此,我的解决方案是从原始图像中提取 IPTC 数据,然后将其嵌入到缩略图中。截至目前,一切运行正常,并且生成了缩略图,只是 IPTC 数据没有被复制。我的代码片段如下。谁能看到我缺少的东西吗?这是基于 PHP 手册中 iptcembed() 的示例。哦,是的,我正在 Zend Framework 中工作,但除了处理配置的注册表之外,这是非常简单的 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); 
            }

    }

I am trying to take a photograph and create a thumbnail without losing the IPTC info that contains copyright info and other information. I am scripting using GD for the resize which of course results in the loss of the IPTC data since it creates an entire new file, not actually resizing the original. So, my solution was to extract the IPTC data from the original image and then embed it in the thumbnail. As of now, everything runs fine and the thumbnail is generated except the IPTC data is not copied over. My code snippet is below. Can anyone see anyhting I am missing? This is based off of the examples in the PHP manual for iptcembed(). Oh yeah, I am working within the Zend Framework but apart from the Registry to handle the config, this is pretty straightforward OOP code. Thanks!

        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); 
            }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2024-11-05 02:25:26

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

返回值
如果成功且假脱机标志低于 2,则 JPEG 将不会作为字符串返回,错误则为 FALSE。

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

I think iptcembed does automatically save the file, which means that the php.net manual example is wrong:

Return Values
If success and spool flag is lower than 2 then the JPEG will not be returned as a string, FALSE on errors.

See http://php.net/iptcembed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文