Resizing images

I have a little code that takes our image size and rescales the image to end up 150 x 230 px minimum while maintaining the aspect ratio. Our PHP Version is 5.3.3
The code is below. For some reason we cant get it to work…

// Fixed Size Thumbnails -  ****
	
	if ($this->_width > 835 || $this->_height > 1280) {
			imagecopyresampled($temp, $this->getScratchResource(), 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
	} else {
		$source = getimagesize($this->getFilePath());
		$srcX = $width;
		$srcY = $height;
		
		if (($source[0]*$srcY) > ($source[1]*$srcX)) {
			$srcX = ($height / $source[1]) * $source[0];
			$offsetX = ($width - $srcX) / 2;
		} else {
			$srcY = ($width / $source[0]) * $source[1];
			$offsetY = ($height - $srcY) / 2; ;
		}
		
		imagecopyresampled($temp, $this->getScratchResource(), $offsetX, $offsetY, 0, 0, $srcX, $srcY, $this->_width, $this->_height);
		
	}
	
	// End Fixed Size Thumbnails code ****

Hi ramartijr,

You are attempting to use an image resource ($temp) that doesn’t appear to have been created. Also, are you declaring $height and $width prior to to code you posted and are you sure they are available to this code?

If $height and $width are previously declared and available, try this:[php]$temp = imagecreatetruecolor($width, $height);
imagecopyresampled($temp, $this->getScratchResource(), 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
[/php]
And[php]$temp = imagecreatetruecolor($srcX, $srcY);
imagecopyresampled($temp, $this->getScratchResource(), $offsetX, $offsetY, 0, 0, $srcX, $srcY, $this->_width, $this->_height);[/php]

You may need to post the code for your $this object if the above doesn’t get you on the right path.

Thank you for your help Malasho. That didnt seem to work.
Here is the code including the $this object.
/**
* Rotates the scratch by given degrees. No cropping will occurr - the canvas will be resized to fit the new boundaries of the image if necessary.
*
* @param float $degrees Angle in degrees to rotate the image. Positive angles indicate a clockwise rotation, negative values are counter-clockwise.
* @param mixed $background Specifies the color of the uncovered zone after the rotation
*/
public function rotateScratch($degrees, $background = 0)
{
$this->_scratchResource = imagerotate($this->_scratchResource, $degrees, $background);
$this->_width = imagesx($this->getScratchResource());
$this->_height = imagesy($this->getScratchResource());
}

public function isScratchTrueColor()
{
	return imageistruecolor($this->getScratchResource());
}

/**
* Applies sharpening to the current scratch
*
* @return bool Returns TRUE on success or FALSE on failure
*/
public function sharpenScratch()
{
	// this needs a $strength parameter but I don't know how matrix convultions work

	$matrix = array(
		array(-1, -1, -1),
		array(-1, 16, -1),
		array(-1, -1, -1)
	);

	$divisor = 8;
	$offset = 0;

	return imageconvolution($this->getScratchResource(), $matrix, $divisor, $offset);
}

/**
* Create a blank true-colour image resource with alpha settings enabled
*
* @param int $width
* @param int $height
* @return resource
*/
protected function _getBlankImageResource($width, $height)
{
	$resource = imagecreatetruecolor($width, $height);
	imagealphablending($resource, false);
	imagesavealpha($resource, true);
	return $resource;
}

/**
* Resample the scratch to a specific width and height -- will stretch the image, ignoring aspect ratios. To keep aspect ratios intact, use other functions like resampleScratchLongEdge and resampleScratchToMaximumDimensions
*
* @param mixed $width
* @param mixed $height
*/
public function resampleScratch($width, $height) {
	
	if ($this->isScratchTrueColor()) {
		$temp = $this->_getBlankImageResource($width, $height);
	} else {
		// determine fill background, transparent if possible
		$fill = imagecolortransparent($this->getScratchResource());
		if ($fill === -1) {
			// no transparency found, find the closest colour to white in the palette and use that as the background
			$fill = imagecolorclosest($this->getScratchResource(), 255, 255, 255);
		}

		$temp = imagecreate($width, $height);
		imagepalettecopy($temp, $this->getScratchResource());
		imagecolortransparent($temp, imagecolortransparent($this->getScratchResource())); // set transparency of new image to same as old image
		imagefill($temp, 0, 0, $fill);
	}
/**	// Fixed Size Thumbnails -  ****

	if ($this->_width > 835 || $this->_height > 1280) {
			imagecopyresampled($temp, $this->getScratchResource(), 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
	} else {
		$source = getimagesize($this->getFilePath());
		$srcX = $width;
		$srcY = $height;
		
		if (($source[0]*$srcY) > ($source[1]*$srcX)) {
			$srcX = ($height / $source[1]) * $source[0];
			$offsetX = ($width - $srcX) / 2;
		} else {
			$srcY = ($width / $source[0]) * $source[1];
			$offsetY = ($height - $srcY) / 2; ;
		}
		
		imagecopyresampled($temp, $this->getScratchResource(), $offsetX, $offsetY, 0, 0, $srcX, $srcY, $this->_width, $this->_height);
		
	}
	
	// End Fixed Size Thumbnails code ****
	*/
	// imagecopyresampled($temp, $this->getScratchResource(), 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);

	$this->_scratchResource = $temp;
	$this->_width = $width;
	$this->_height = $height;
}

/**
* Returns a GD image resource for the current scratch
*
*/
public function getScratchResource()
{
	return $this->_scratchResource;
}

/**
* Overlay another GD resource onto the current scratch at the given offset
*
* @param ISC_IMAGE_LIBRARY_GD $image
* @param mixed $offsetX
* @param mixed $offsetY
*/
public function overlayImage(ISC_IMAGE_LIBRARY_GD $image, $offsetX, $offsetY)
{
	// when watermarking is implemented this may need to be re-examined for when gifs or 8bit pngs are used

	$overlayResource = $image->getScratchResource();
	$overlayWidth = $image->getWidth();
	$overlayHeight = $image->getHeight();

	if ($offsetX < 0) {
		$offsetX = $this->getWidth() + $offsetX - $overlayWidth;
	}

	if ($offsetY < 0) {
		$offsetY = $this->getHeight() + $offsetY - $overlayHeight;
	}

	imagecopyresampled($this->getScratchResource(), $overlayResource, $offsetX, $offsetY, 0, 0, $overlayWidth, $overlayHeight, $overlayWidth, $overlayHeight);
}

}

Thanks for posting the rest of the code. It looks like all three variables are being set, so my first comments are no longer applicable.

I see is that you are not closing your comment tag for your Fixed Size Thumbnails section. This means that none of that code will execute. I have uncommented it below, lets try this next…[php]/**

  • Rotates the scratch by given degrees. No cropping will occurr - the canvas will be resized to fit the new boundaries of the image if necessary.
  • @param float $degrees Angle in degrees to rotate the image. Positive angles indicate a clockwise rotation, negative values are counter-clockwise.
  • @param mixed $background Specifies the color of the uncovered zone after the rotation
    */
    public function rotateScratch($degrees, $background = 0)
    {
    $this->_scratchResource = imagerotate($this->_scratchResource, $degrees, $background);
    $this->_width = imagesx($this->getScratchResource());
    $this->_height = imagesy($this->getScratchResource());
    }

public function isScratchTrueColor()
{
return imageistruecolor($this->getScratchResource());
}

/**

  • Applies sharpening to the current scratch

  • @return bool Returns TRUE on success or FALSE on failure
    */
    public function sharpenScratch()
    {
    // this needs a $strength parameter but I don’t know how matrix convultions work

    $matrix = array(
    array(-1, -1, -1),
    array(-1, 16, -1),
    array(-1, -1, -1)
    );

    $divisor = 8;
    $offset = 0;

    return imageconvolution($this->getScratchResource(), $matrix, $divisor, $offset);
    }

/**

  • Create a blank true-colour image resource with alpha settings enabled
  • @param int $width
  • @param int $height
  • @return resource
    */
    protected function _getBlankImageResource($width, $height)
    {
    $resource = imagecreatetruecolor($width, $height);
    imagealphablending($resource, false);
    imagesavealpha($resource, true);
    return $resource;
    }

/**

  • Resample the scratch to a specific width and height – will stretch the image, ignoring aspect ratios. To keep aspect ratios intact, use other functions like resampleScratchLongEdge and resampleScratchToMaximumDimensions

  • @param mixed $width

  • @param mixed $height
    */
    public function resampleScratch($width, $height) {

    if ($this->isScratchTrueColor()) {
    $temp = $this->_getBlankImageResource($width, $height);
    } else {
    // determine fill background, transparent if possible
    $fill = imagecolortransparent($this->getScratchResource());
    if ($fill === -1) {
    // no transparency found, find the closest colour to white in the palette and use that as the background
    $fill = imagecolorclosest($this->getScratchResource(), 255, 255, 255);
    }

    $temp = imagecreate($width, $height);
    imagepalettecopy($temp, $this->getScratchResource());
    imagecolortransparent($temp, imagecolortransparent($this->getScratchResource())); // set transparency of new image to same as old image
    imagefill($temp, 0, 0, $fill);
    

    }
    /** // Fixed Size Thumbnails - ****/

    if ($this->_width > 835 || $this->_height > 1280) {
    imagecopyresampled($temp, $this->getScratchResource(), 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);
    } else {
    $source = getimagesize($this->getFilePath());
    $srcX = $width;
    $srcY = $height;

    if (($source[0]*$srcY) > ($source[1]*$srcX)) {
       $srcX = ($height / $source[1]) * $source[0];
       $offsetX = ($width - $srcX) / 2;
    } else {
       $srcY = ($width / $source[0]) * $source[1];
       $offsetY = ($height - $srcY) / 2; ;
    }
    
    imagecopyresampled($temp, $this->getScratchResource(), $offsetX, $offsetY, 0, 0, $srcX, $srcY, $this->_width, $this->_height);
    

    }

    // End Fixed Size Thumbnails code ****
    // imagecopyresampled($temp, $this->getScratchResource(), 0, 0, 0, 0, $width, $height, $this->_width, $this->_height);

    $this->_scratchResource = $temp;
    $this->_width = $width;
    $this->_height = $height;
    }

/**

  • Returns a GD image resource for the current scratch

*/
public function getScratchResource()
{
return $this->_scratchResource;
}

/**

  • Overlay another GD resource onto the current scratch at the given offset

  • @param ISC_IMAGE_LIBRARY_GD $image

  • @param mixed $offsetX

  • @param mixed $offsetY
    */
    public function overlayImage(ISC_IMAGE_LIBRARY_GD $image, $offsetX, $offsetY)
    {
    // when watermarking is implemented this may need to be re-examined for when gifs or 8bit pngs are used

    $overlayResource = $image->getScratchResource();
    $overlayWidth = $image->getWidth();
    $overlayHeight = $image->getHeight();

    if ($offsetX < 0) {
    $offsetX = $this->getWidth() + $offsetX - $overlayWidth;
    }

    if ($offsetY < 0) {
    $offsetY = $this->getHeight() + $offsetY - $overlayHeight;
    }

    imagecopyresampled($this->getScratchResource(), $overlayResource, $offsetX, $offsetY, 0, 0, $overlayWidth, $overlayHeight, $overlayWidth, $overlayHeight);
    }
    }[/php]

If that doesn’t work, can you also include an example of the code you are using to test the routines.

Sponsor our Newsletter | Privacy Policy | Terms of Service