Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image Data Scream to a Variable

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
In this snippet from a function I am working on, it is outputting the image data to the browser when I am trying to create a variable containing the data. Otherwise it is working as expected so what did I miss? Right now it is sending the image data to the screen and expecting headers which, of course, are not being sent.

PHP:
imagecopyresampled($tmp, $src, 0, 0, 0, 0,$newWidth, $newHeight, $Imgwidth, $Imgheight);
	  
switch ($ImgType) :
     case 1 : $result = imagegif($tmp);  break;
     case 2 : $result = imagejpeg($tmp, null, 100); break;
     case 3 : $result = imagepng($tmp, null);  break;
     case 6 : $result = imagejpeg($tmp, null, 100); break;
     default : $result = imagejpeg($tmp, null, 100);
endswitch;

In case it helps, here is the complete function which itself uses a few other custom functions all of which are working:

PHP:
function ConvertImage($ImgFile, $ImgCategory) {
    if (file_exists($ImgFile)) {
        list($Imgwidth, $Imgheight, $ImgType, $attr) = getimagesize($ImgFile);

        switch ($ImgType) :
            case 1 : $src = imagecreatefromgif($ImgFile);  break;
            case 2 : $src = imagecreatefromjpeg($ImgFile); break;
            case 3 : $src = imagecreatefrompng($ImgFile);  break;
            case 6 : $src = imagecreatefrombmp($ImgFile); break; // Custom function
            default : 
		$images->Image->Errors->AddError("<font color=\"red\">Only JPG, GIF, BMP or PNG format images may be used.</font>");
		$images_OnValidate = 0;
        endswitch;

		if ($src === FALSE) :
			$images->Image->Errors->AddError("<font color=\"red\">Unable to create image.</font>");
			$images_OnValidate = 0;
		endif;

        switch ($ImgCategory) :
            case 1 : $MaxDim = 200;  break;
            case 5 : $MaxDim = 200; break;
            case 6 : $MaxDim = 100;  break;
            default : $MaxDim = 200;
        endswitch;
		
        if($Imgheight > $MaxDim):
            $newHeight = $MaxDim;
	    $newWidth = round( $Imgwidth * ($newHeight/$Imgheight));
        else:
           $newHeight = $Imgheight;
           $newWidth = $Imgwidth;
		endif;
		
		$tmp = imagecreatetruecolor($newWidth, $newHeight);
			if ($ImgType == 1):
				$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
				imagefill($tmp, 0, 0, $transparent);
				imagealphablending($tmp, FALSE);
				imagesavealpha($tmp, TRUE);
			endif;
			
       [COLOR=red]imagecopyresampled($tmp, $src, 0, 0, 0, 0,$newWidth, $newHeight, $Imgwidth, $Imgheight);
	   
	switch ($ImgType) :
            case 1 : $result = imagegif($tmp);  break;
            case 2 : $result = imagejpeg($tmp, null, 100); break;
            case 3 : $result = imagepng($tmp, null);  break;
            case 6 : $result = imagejpeg($tmp, null, 100); break;
            default : $result = imagejpeg($tmp, null, 100);
	endswitch;[/color]
	
	imagedestroy($tmp);

	}
    return $result;
}
 
I just noticed my typo in the title! It should be Image STREAM to a Variable!
 
Never mind! I tried all afternoon but as soon as I posted the question, I figured it out:

PHP:
imagecopyresampled($tmp, $src, 0, 0, 0, 0,$newWidth, $newHeight, $Imgwidth, $Imgheight);

	ob_start();
		switch ($ImgType) :
			case 1 : imagegif($tmp);  break;
			case 2 : imagejpeg($tmp, null, 100); break;
			case 3 : imagepng($tmp, null);  break;
			case 6 : imagejpeg($tmp, null, 100); break;
			default : imagejpeg($tmp, null, 100);
		endswitch;
	   $result = ob_get_contents();
	ob_end_clean();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top