代码之家  ›  专栏  ›  技术社区  ›  dfilkovi

php调整图像大小脚本

  •  1
  • dfilkovi  · 技术社区  · 16 年前

    我认为这是一个图片来源。在该数据中始终存在一个字符串 CREATOR: gd-jpeg v1.0 所以我想知道这是什么?我用@sign禁用了任何错误输出。

    这是代码,我也禁用了错误输出,因为没有任何错误,我认为禁用错误输出将禁用jiberish数据,但不管怎样都会显示数据。

    代码:

    <?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;
    
    
    ?>
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   Alix Axel    16 年前

    在顶部添加以下内容:

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

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

        2
  •  1
  •   NullUserException Mark Roddy    14 年前

    首先要移除错误抑制,看看是否有错误。查看一些代码也会有所帮助。

    好吧,现在修复问题已经太迟了,但这里有另一个关于代码的建议。所有那些“readdir,decision if file,build path”的东西都是很难使用的。请尝试以下选项:

    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 SPL 虽然它们一开始使用起来有些费解,但一旦您理解了它们,它们可以使开发变得更加容易。使用以上内容,您现在可以循环使用$image,并从path下面的所有目录中获取以.gif、.jpg或.png结尾的所有文件名,如下所示:

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

    SplFileInfo 对象,因此您还可以使用它获得许多有用的其他方法。

    推荐文章