Jump to content

Image Thumbnail/Watermark Broken in PHP 4.3.2


Temujin_12

Recommended Posts

The server I was hosting my site on (for free so I can't complain too much)  crashed and the administrators migrated to a new server.  PHP 4.3.2 is on the new server (can't remember what was on the old).

 

I have the following script which thumbnails and/or watermarks an image and outputs it back to the browser:

<?php

ini_set("memory_limit", "16M");

// watermarks an image
// params	$sourcefile - image to watermark (gd resource or filename)
//		$watermarkfile - image to use as watermark (gd resource or filename)
//		$fileType - type of image ('gif', 'png', 'jpg')
// returns	gd image resource
// NOTE:	if passing resource for $sourcefile you must also pass 3rd parameter
function watermark(&$sourcefile, &$watermarkfile, $fileType="") {
// either create GD resource from path to png, or use passed in GD resource
if (gettype($watermarkfile) == "string") $watermarkfile_id = imagecreatefrompng($watermarkfile);
else $watermarkfile_id = $watermarkfile;

imageAlphaBlending($watermarkfile_id, false);
imageSaveAlpha($watermarkfile_id, true);

// either create GD resource from path to src image, or use passed in GD resource
if (gettype($sourcefile) == "string") {
	$fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3));

	switch($fileType) {
		case('gif'):
			$sourcefile_id = imagecreatefromgif($sourcefile);
			break;

		case('png'):
			$sourcefile_id = imagecreatefrompng($sourcefile);
			break;

		default:
			$sourcefile_id = imagecreatefromjpeg($sourcefile);
	} // end switch
} // end if
else $sourcefile_id = $sourcefile;

//Get the sizes of both pix 
$sourcefile_width=imageSX($sourcefile_id);
$sourcefile_height=imageSY($sourcefile_id);
$watermarkfile_width=imageSX($watermarkfile_id);
$watermarkfile_height=imageSY($watermarkfile_id);

$dest_x = ( $sourcefile_width / 2 ) - ( $watermarkfile_width / 2 );
$dest_y = ( $sourcefile_height / 2 ) - ( $watermarkfile_height / 2 );

// if a gif, we have to upsample it to a truecolor image
if ($fileType == 'gif') {
	// create an empty truecolor container
	$tempimage = imagecreatetruecolor($sourcefile_width, $sourcefile_height);

	// copy the 8-bit gif into the truecolor image
	imagecopy($tempimage, $sourcefile_id, 0, 0, 0, 0, $sourcefile_width, $sourcefile_height);

	// copy the source_id int
	$sourcefile_id = $tempimage;
} // end if

imagecopy($sourcefile_id, $watermarkfile_id, $dest_x, $dest_y, 0, 0,
		$watermarkfile_width, $watermarkfile_height);


return $sourcefile_id;
} // end watermark function

// proportionally resizes image to fit into $max_width AND $max_height
// params:	$resource - filename<string> or gd image resource of image to resize
//		$max_width - <int>
//		$max_height - <int>
// returns	gd image resource of resulting resized image
function resizeImage(&$resource, $max_width, $max_height) {
if (gettype($resource) == "string") list($orig_width, $orig_height) = getimagesize($resource);
else {
	$orig_width = imagesx($resource);
	$orig_height = imagesy($resource);
} // end else

$width = $orig_width;
$height = $orig_height;

# taller
if ($height > $max_height) {
	$width = ($max_height / $height) * $width;
	$height = $max_height;
} // end if

# wider
if ($width > $max_width) {
	$height = ($max_width / $width) * $height;
	$width = $max_width;
} // end if
$image_p = imagecreatetruecolor($width, $height);

// if filename passed in then load gd resource
if (gettype($resource) == "string") {
	$fileType = strtolower(substr($resource, strlen($resource)-3));
	switch ($fileType) {
		case('gif'):
			$resource = imagecreatefromgif($resource);
			break;

		case('png'):
			$resource = imagecreatefrompng($resource);
			break;

		default:
			$resource = imagecreatefromjpeg($resource);
	} // end switch
} // end if

imagecopyresampled($image_p, $resource, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);

return $image_p;
} // end resizeImage

// do resizing and watermarking
if ($_GET) {
// get full server path to image
require_once 'DataObject_Connect.php';
require_once 'DataObjects/Session.php';
$session = new DataObjects_Session();
$session->get($_GET['session_id']);
$image_src = $session->getImagePath($_GET['filename'], true);

// determine resizing
switch ($_GET['size']) {
	case 'thumb':
		$resource = resizeImage($image_src, 100, 100);
	break;
	case 'regular':
		$resource = resizeImage($image_src, 292, 292);
	break;
	default:
		$watermark_src = $_SERVER["DOCUMENT_ROOT"]."/images/watermark.png";
		$resource = watermark($image_src, $watermark_src);
} // end switch

// output
header("Content-type: image/jpg");
imagejpeg ($resource, "", 100);
} // end if


?>

 

It worked on the old server but doesn't on the new server (PHP 4.3.2).  It looks like it is outputting data (ie: when I view the source it has binary data), but the images show up as broken and if I save it out to a file (either on the user using imagejpeg($resource, "/path/to/file", 100) or saving from the browser) any image manipulation program I try is unable to view it.

 

Any ideas as to why the above script wouldn't work for PHP 4.3.2?

 

I'm open to completely replacing my resizing/watermarking routines as long as they are functionally equivalent.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.