Jump to content

About imagecreatetruecolor function creating "Black" image


Ab

Recommended Posts

 

 

Hi All,

 

I was working on class that manipulates images and I am having a trouble loading the image, i.e my program is loading "black" image isntead of

image thumbnail, I am not sure what I am getting wrong but I would appreciate if someone would point me out what I am getting wrong or way to get around this problem ( I thought imagecreatetruecolor would create the exact image ):

 

 

Here is the test code:

 


require_once('MarkImageError.php');
require_once('Mark_Images.php');

$img = new MarkImages(400,400);
$img->getImage('_Images/img005.jpg');
header ( 'Content-type: '.$img->getMime());
$img->buildThumbnail();

 

Here is the class partially omitted:

 

....omitted
........
..........
public function getImage($image)
{	
	if (!$imgProperties = @getimagesize($image))
	{
		Throw new ImageFileException ('Could not find image: '.$image);
	}

	if (in_array($imgProperties['mime'],$this->types)) // check if image type is acceptable
	{
		$loader = $this->imgLoaders[$imgProperties['mime'] ]; //load the image using the MIME type
		$this->source = $loader($image);  
		$this->sourceWidth = $imgProperties[0]; //get the image width
		$this->sourceHeight = $imgProperties[1];  //get the image Height
		$this->sourceMime = $imgProperties['mime']; // get the Mime type

		// fort testing only
		/*echo 'source: '.$this->source.'<br/>';
		echo 'source width: '.$this->sourceWidth.'<br/>';
		echo 'source height: '.$this->sourceHeight.'<br/>';
		echo 'source mime: '.$this->sourceMime.'<br/>';
		*/
		$this->initializeImage();

		return true;

	}
	else
	{
		throw new ImagetypeNotSupported ('Image Mime type'.$imgProperties['mime'].'not supported'  );
	}
}

        public function buildThumbnail($file = null)
{
	$creator = $this->imgCreators[$this->sourceMime];

	if(isset($file))
	{
		return $creator($this->thumb, $file);
	}
	else
	{			
		return $creator($this->thumb);

	}
}

   
        public function initializeImage()
{
	if ($this->scale)
	{
		if ($this->sourceWidth > $this->sourceHeight)
		{
			$this->thumbWidth = $this->maxWidth;
			$this->thumbHeight = floor ($this->sourceHeight * ($this->maxWidth/$this->sourceWidth));
		}
		else if ($this->sourceWidth < $this->sourceHeight)
		{
			$this->thumbHeight = $this->maxHeight;
			$this->thumbWidth = floor ($this->sourceWidth * ($this->maxHeight/$this->sourceHeight));
		}
		else
		{
			$this->thumbWidth   = $this->maxHeight;
			$$this->thumbHeight = $this->maxWidth;
		}
	}

	//echo 'source width: '.$this->thumbWidth.'<br/>';
	//echo 'source height: '.$this->thumbHeight.'<br/>';

	$this->thumb = imagecreatetruecolor( $this->thumbWidth, $this->thumbHeight );

	if ($this->sourceWidth <= $this->maxWidth && 
	                          $this->sourceHeight <= $this->maxHeight &&
	                          $this->inflate == false )
	                          
	  {// this goes with the if...
	                          	
	      $this->thumb = $this->source;                          	
	                          	
	  }
	  else
	  {
	  	@imagecopyresampled($this->thumb,$this->source,0,0,0,0,$this->thumbWidth,$this->thumbHeight &&
	  	                                  $this->sourceWidth, $this->sourceHeight );
	  	                                  
	  	
	  	
	  	
	  	                                  
	  	                                 
	  }
}



 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.