php调整图像大小脚本

发布于 2024-08-15 08:31:18 字数 5401 浏览 3 评论 0原文

我正在使用目录迭代器来迭代目录并调整在该目录中找到的图像的大小,我从浏览器执行此操作,因为我没有对该服务器的 ssh 访问权限。 大多数图片调整大小都很好,但每 10 张图片(或多或少)我都会得到乱码数据。 我认为这是一个图片来源。在该数据中总是有一个字符串 CREATOR: gd-jpeg v1.0 所以我想知道这是什么?我用@符号禁用了任何错误输出。

编辑:

这是代码,而且我还禁用了错误输出,因为没有任何错误,我认为禁用错误输出会禁用此乱码数据,但无论如何都会显示数据。

代码:

<?php
/*
big = 350
thumb = 90
thumb2 = 70
top = 215
*/

set_time_limit(0);
ini_set('memory_limit', '128M');
ini_set('display_errors', 'On'); 

class ResizeImages
{
    private $dir = 'images/articles_backup_2009-12-19';
    private $imageType = array(
        '_big' => 'h:350',
        '_thumb' => 'm:90',
        '_thumb2' => 'h:70',
        '_top' => 'h:215'
    );

    public function __construct()
    {
        $this->deleteImages();
        $this->resizeImages();
    }

    private function resizeImages()
    {
        $n = 0;
        $dh = opendir($this->dir);
        while (($file = readdir($dh)) !== false) 
        {
            if(is_dir($this->dir."/".$file) && $file != '.' && $file != '..')
            {   
                echo $this->dir."/".$file.'<br />';
                $deldir = opendir($this->dir."/".$file);
                while (($filedel = readdir($deldir)) !== false) 
                {
                    if ($filedel != '.' && $filedel != '..' && $filedel != 'Thumbs.db') 
                    {
                        $val = $this->resize($this->dir."/".$file."/".$filedel);
                        $n++;
                    }
                }
            }
        }
        closedir($dh);
    }

    private function resize($target)
    {
        $img = $target;

        $origSize = getimagesize($img);
        $origWidth = $origSize[0];
        $origHeight = $origSize[1];

        foreach($this->imageType as $key=>$value)
        {
            $attr = explode(':', $value);

            if(strpos($attr[0], 'w') !== false) 
            {
                $this->imageWidth = $attr[1];
                $this->imageHeight = false;
            }
            if(strpos($attr[0], 'h') !== false) 
            {
                $this->imageHeight = $attr[1];
                $this->imageWidth = false;
            }

            $imageTmp = explode('.', $img);
            if(count($imageTmp) == 2) $image_name_fin = $imageTmp[0].$key.'.'.$imageTmp[1];
            else if(count($imageTmp) == 4) $image_name_fin = $imageTmp[0].'.'.$imageTmp[1].$key.'.'.$imageTmp[2];

            if($this->imageWidth != false) 
            {
                if($origWidth <= $this->imageWidth)
                {
                    $resizeHeight = $origHeight;
                    $resizeWidth = $origWidth;
                }
                else
                {
                    $resizeHeight = round($origHeight / ($origWidth / $this->imageWidth));
                    $resizeWidth = $this->imageWidth;
                }
            }
            else if($this->imageHeight != false) 
            {
                if($origHeight <= $this->imageHeight)
                {
                    $resizeHeight = $origHeight;
                    $resizeWidth = $origWidth;
                }
                else
                {
                    $resizeWidth = round($origWidth / ($origHeight / $this->imageHeight));
                    $resizeHeight = $this->imageHeight;
                }
            }

            $im = ImageCreateFromJPEG ($img) or // Read JPEG Image
            $im = ImageCreateFromPNG ($img) or // or PNG Image
            $im = ImageCreateFromGIF ($img) or // or GIF Image
            $im = false; // If image is not JPEG, PNG, or GIF

            if (!$im) 
            {
                $this->error = array(
                    'error' => true,
                    'notice' => 'UPLOADUNSUCCESSFULL'
                );
                return $this->error;
            }

            $thumb = ImageCreateTrueColor ($resizeWidth, $resizeHeight);
            ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $origWidth, $origHeight);
            ImageJPEG ($thumb, $image_name_fin, $this->imageQuality);
            //echo $image_name_fin.'<br />';
        }

        $this->error = array(
            'imageUrl' => $image_name,
            'error' => false,
            'notice' => 'IMAGEUPLOADED'
        );
        return $this->error;            
    }

    private function deleteImages()
    {
        $dh = opendir($this->dir);
        while (($file = readdir($dh)) !== false) 
        {
            if(is_dir($this->dir."/".$file))
            {
                //echo $file.'<br />';
                $deldir = opendir($this->dir."/".$file);
                while (($filedel = readdir($deldir)) !== false) 
                {
                    if(strpos($this->dir."/".$file."/".$filedel, '_big.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb2.') !== false || strpos($this->dir."/".$file."/".$filedel, '_top.') !== false)
                    {
                        unlink($this->dir."/".$file."/".$filedel);
                    }
                }
            }
        }
        closedir($dh);
    }
}

$batch = new ResizeImages;


?>

I am using directory iterator to iterate through directories and resize images found in that directory, I am doing this from browser cause I don't have ssh access to that server.
Most pictures resize fine but for every 10 pictures (more or less) I get jiberish data out.
I think it's a picture source. in that data there is always a string CREATOR: gd-jpeg v1.0 so I'm wondering what is this? I disabled any error output with @ sign.

EDIT:

Here is the code, and also I disabled error output cause there aren't any errors and I thought that disabling error output would disable this jiberish data, but data is displayed no matter.

Code:

<?php
/*
big = 350
thumb = 90
thumb2 = 70
top = 215
*/

set_time_limit(0);
ini_set('memory_limit', '128M');
ini_set('display_errors', 'On'); 

class ResizeImages
{
    private $dir = 'images/articles_backup_2009-12-19';
    private $imageType = array(
        '_big' => 'h:350',
        '_thumb' => 'm:90',
        '_thumb2' => 'h:70',
        '_top' => 'h:215'
    );

    public function __construct()
    {
        $this->deleteImages();
        $this->resizeImages();
    }

    private function resizeImages()
    {
        $n = 0;
        $dh = opendir($this->dir);
        while (($file = readdir($dh)) !== false) 
        {
            if(is_dir($this->dir."/".$file) && $file != '.' && $file != '..')
            {   
                echo $this->dir."/".$file.'<br />';
                $deldir = opendir($this->dir."/".$file);
                while (($filedel = readdir($deldir)) !== false) 
                {
                    if ($filedel != '.' && $filedel != '..' && $filedel != 'Thumbs.db') 
                    {
                        $val = $this->resize($this->dir."/".$file."/".$filedel);
                        $n++;
                    }
                }
            }
        }
        closedir($dh);
    }

    private function resize($target)
    {
        $img = $target;

        $origSize = getimagesize($img);
        $origWidth = $origSize[0];
        $origHeight = $origSize[1];

        foreach($this->imageType as $key=>$value)
        {
            $attr = explode(':', $value);

            if(strpos($attr[0], 'w') !== false) 
            {
                $this->imageWidth = $attr[1];
                $this->imageHeight = false;
            }
            if(strpos($attr[0], 'h') !== false) 
            {
                $this->imageHeight = $attr[1];
                $this->imageWidth = false;
            }

            $imageTmp = explode('.', $img);
            if(count($imageTmp) == 2) $image_name_fin = $imageTmp[0].$key.'.'.$imageTmp[1];
            else if(count($imageTmp) == 4) $image_name_fin = $imageTmp[0].'.'.$imageTmp[1].$key.'.'.$imageTmp[2];

            if($this->imageWidth != false) 
            {
                if($origWidth <= $this->imageWidth)
                {
                    $resizeHeight = $origHeight;
                    $resizeWidth = $origWidth;
                }
                else
                {
                    $resizeHeight = round($origHeight / ($origWidth / $this->imageWidth));
                    $resizeWidth = $this->imageWidth;
                }
            }
            else if($this->imageHeight != false) 
            {
                if($origHeight <= $this->imageHeight)
                {
                    $resizeHeight = $origHeight;
                    $resizeWidth = $origWidth;
                }
                else
                {
                    $resizeWidth = round($origWidth / ($origHeight / $this->imageHeight));
                    $resizeHeight = $this->imageHeight;
                }
            }

            $im = ImageCreateFromJPEG ($img) or // Read JPEG Image
            $im = ImageCreateFromPNG ($img) or // or PNG Image
            $im = ImageCreateFromGIF ($img) or // or GIF Image
            $im = false; // If image is not JPEG, PNG, or GIF

            if (!$im) 
            {
                $this->error = array(
                    'error' => true,
                    'notice' => 'UPLOADUNSUCCESSFULL'
                );
                return $this->error;
            }

            $thumb = ImageCreateTrueColor ($resizeWidth, $resizeHeight);
            ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $resizeWidth, $resizeHeight, $origWidth, $origHeight);
            ImageJPEG ($thumb, $image_name_fin, $this->imageQuality);
            //echo $image_name_fin.'<br />';
        }

        $this->error = array(
            'imageUrl' => $image_name,
            'error' => false,
            'notice' => 'IMAGEUPLOADED'
        );
        return $this->error;            
    }

    private function deleteImages()
    {
        $dh = opendir($this->dir);
        while (($file = readdir($dh)) !== false) 
        {
            if(is_dir($this->dir."/".$file))
            {
                //echo $file.'<br />';
                $deldir = opendir($this->dir."/".$file);
                while (($filedel = readdir($deldir)) !== false) 
                {
                    if(strpos($this->dir."/".$file."/".$filedel, '_big.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb.') !== false || strpos($this->dir."/".$file."/".$filedel, '_thumb2.') !== false || strpos($this->dir."/".$file."/".$filedel, '_top.') !== false)
                    {
                        unlink($this->dir."/".$file."/".$filedel);
                    }
                }
            }
        }
        closedir($dh);
    }
}

$batch = new ResizeImages;


?>

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

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

发布评论

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

评论(2

你在我安 2024-08-22 08:31:18

在顶部添加以下内容:

error_reporting(E_ALL);

并尝试更改此内容:

$im = ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = ImageCreateFromPNG ($img) or // or PNG Image
$im = ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF

对此:

$im = ImageCreateFromString(file_get_contents($img));

有帮助吗?损坏的图像上是否有任何图案?它们都是同一类型吗?

At the top add this:

error_reporting(E_ALL);

And try changing this:

$im = ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = ImageCreateFromPNG ($img) or // or PNG Image
$im = ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF

To this:

$im = ImageCreateFromString(file_get_contents($img));

Did it help? Also is there any pattern on the corrupted images? Are they all of the same type?

寂寞清仓 2024-08-22 08:31:18

那么,第一件事就是取消错误抑制,看看是否有错误。查看您的一些代码也可能会有所帮助。

编辑
好吧,解决你的问题已经太晚了,但这里有另一个针对你的代码的建议。所有“readdir、决定是否文件、构建路径”的东西使用(和查看)都很痛苦。试试这个替代方案:

class ImageFilterIterator extends FilterIterator
{
    public function accept()
    {
        $ext  = pathinfo($this->current()->getRealPath(), PATHINFO_EXTENSION);
        return stripos('.gif|.jpg|.png', $ext);
    }
}

$path   = '.';
$images = new ImageFilterIterator(
              new RecursiveIteratorIterator(
                  new RecursiveDirectoryIterator($path)));

迭代器 来自 SPL 虽然一开始使用它们有些令人困惑,但一旦您理解了它们,它们就可以使开发变得更加容易。通过上面的内容,您现在可以循环 $image 并从路径下的所有目录中获取所有以 .gif、.jpg 或 .png 结尾的文件名,如下所示:

foreach($images as $image) {
    echo $image;
}

事实上,$image 不仅仅是一个字符串,而是一个 SplFileInfo 对象,因此您还可以使用它获得一堆有用的其他方法。

Well, the first thing would be to remove the error suppression to see if there is any errors. Seeing some of your code could be helpful as well.

EDIT
Ok, too late to fix your problem, but here is another suggestion for your code. All that "readdir, decide if file, build path" stuff is just a pain to use (and look at). Try this for an alternative:

class ImageFilterIterator extends FilterIterator
{
    public function accept()
    {
        $ext  = pathinfo($this->current()->getRealPath(), PATHINFO_EXTENSION);
        return stripos('.gif|.jpg|.png', $ext);
    }
}

$path   = '.';
$images = new ImageFilterIterator(
              new RecursiveIteratorIterator(
                  new RecursiveDirectoryIterator($path)));

Iterators are from the SPL and while they are somewhat puzzling to use at first, they can make development much easier once you understood them. With the above you can now loop over $image and get all filenames ending in .gif, .jpg or .png from all directories below path, like this:

foreach($images as $image) {
    echo $image;
}

In fact, $image is not just a string, but an SplFileInfo object, so you also get a bunch of useful other methods with it.

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