Help with function

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Locked
pbass98

Help with function

Post by pbass98 »

Hi--

I've written a PHP procedure to take an image, convert it into three sizes and upload the three images to an Amazon S3 bucket using a PHP library called Tarzan.

However, the script frequently runs out of memory and sometimes the resulting images are corrupted (far too frequently, lately).

I'd greatly appreciate any experienced programmers giving me advice on how to cut down the memory usage, and if possible, perform some kind of error checking to make sure the image is not corrupted, and if it is, to re-do the resize. Any and all advice welcome.

Here's the function:

Code: Select all

function createthumb($name)
{
    global $temp_path;
    global $bucket;
    global $s3;

    $src_img = new Imagick($temp_path . $name);
    $geo = $src_img->getImageGeometry();
    
	$old_x = $geo['width'];
	$old_y = $geo['height'];


	if ($old_x > $old_y)
	{
		$thumb_w=200;
		$thumb_h=0;
        $fs_w=500;
		$fs_h=0;
        $wt_w=800;
		$wt_h=0;
	} elseif ($old_x < $old_y)	{
		$thumb_w=0;
		$thumb_h=200;
        $fs_w=0;
		$fs_h=500;
        $wt_w=0;
		$wt_h=500;
	} elseif ($old_x == $old_y)	{
		$thumb_w=200;
		$thumb_h=200;
        	$fs_w=500;
        	$fs_h=500;
        	$wt_w=800;
        	$wt_h=800;
	}

	unlink( $temp_path . $name ); // delete the temp images	

	// Output the image
do {
    $i = 0;
    try {
    $tn=$src_img->clone();
	$tn->thumbnailImage($thumb_w,$thumb_h);
	ob_start(); // start output buffer	
    $output = $tn->getimageblob();
    echo $output;
	$tmp_jpg = ob_get_contents();
        $file = $s3->create_object($bucket, array(
                        'filename' => $thumb_path . $name,
                        'body' => "$tmp_jpg",
                        'contentType' => 'image/jpeg',
                        'acl' => S3_ACL_PUBLIC
                        ));
                    if (!$file->isOK()) { 
                        throw new Exception('S3 failure'); 
                    } else {
                        break; // made it this far so we break the do-while loop
                    }
    } catch (Exception $e) {
        $i++;
    }
} while ($i < 3);
// made it through try-catch so purge vars and continue
	ob_end_clean();
	$tn->destroy();
	unset($tmp_jpg);
    unset($output);
    unset($file);

do {
    $i = 0;
    try {
	//output fullsize
	$fs=$src_img->clone();
	$fs->thumbnailImage($fs_w,$fs_h);
	ob_start(); // start output buffer	
    $output = $fs->getimageblob();
    echo $output;
	$tmp_jpg = ob_get_contents();
    $file = $s3->create_object($bucket, array(
                    'filename' => $fullsize_path . $name,
                    'body' => "$tmp_jpg",
                    'contentType' => 'image/jpeg',
                    'acl' => S3_ACL_PUBLIC
                    ));
                    if (!$file->isOK()) {
                        throw new Exception('S3 failure');
                    } else {
                        break; // made it this far so we break the do-while loop
                    }
    } catch (Exception $e) {
        $i++;
    }
} while ($i < 3);
	ob_end_clean();
	$fs->destroy();	
	unset($tmp_jpg);
    unset($output);
    unset($file);

do {
    $i = 0;
    try {
	// output large
	$wt=$src_img->clone();
	$wt->thumbnailImage($wt_w,$wt_h);
	ob_start(); // start output buffer	
    $output = $wt->getimageblob();
    echo $output;
	$tmp_jpg = ob_get_contents();
    $file = $s3->create_object($bucket, array(
                    'filename' => $walkthru_path . $name,
                    'body' => "$tmp_jpg",
                    'contentType' => 'image/jpeg',
                    'acl' => S3_ACL_PUBLIC
                    ));
                    if (!$file->isOK()) {
                        throw new Exception('S3 failure');
                    } else {
                        break; // made it this far so we break the do-while loop
                    }  
    } catch (Exception $e) {
        $i++;
    }
} while ($i < 3);
	ob_end_clean();
	$wt->destroy();
	unset($tmp_jpg);
    unset($output);
    unset($file);

	$src_img->destroy();

}


Locked