php görüntüleri komut dosyası yeniden boyutlandırma

2 Cevap php

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:

İşte kod, ve ayrıca engelli hata çıkışı neden herhangi bir hata yok ve ben hata çıktı devre dışı bırakılması bu jiberish verileri kaldırmak düşündüm, ancak veri olursa olsun görüntülenir.

Kod:

<?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 Cevap

Üstündeki bu ekleyin:

error_reporting(E_ALL);

Ve bu değiştirmeyi deneyin:

$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

Buna:

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

Bu yardım mı? Ayrıca bozuk görüntüler üzerinde herhangi bir desen var mı? Aynı tür tüm var mı?

Peki, ilk şey, herhangi bir hata olup olmadığını görmek için hata bastırma kaldırmak olacaktır. Senin kod bazı görme de yararlı olabilir.

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 SPL vardır ve onlar biraz ilk başta kullanmak şaşırtıcı yaparken bunları anladım bir kez, onlar geliştirme çok daha kolay yapabilirsiniz. Ile size $ görüntünün üzerinde şimdi döngü ve gif, jpg ya da böyle, yolun altındaki tüm dizinleri png ile biten tüm dosya adlarını almak üstünde...:

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

Aslında, $ resim sadece bir dize değil, bir SplFileInfo bir nesne, bu yüzden de onunla yararlı diğer yöntemlerle bir demet olsun.