PNG Issue

Discussion in 'PHP' started by Synchronium, Aug 4, 2009.

  1. #1
    EDIT: Fixed!


    I'm using a php script to produce a different png image depending on the query string input, but it's producing a different result to the actual png.

    Here's the actual png image that should be displayed: [​IMG]

    Here's the image the script outputs: [​IMG]

    Also, the "unrated" image isn't displaying correctly at all. Here's what it's supposed to look like: [​IMG]

    And here's the script output: [​IMG]

    And finally here's the script:

    <?php
    
    	require_once( 'config.php' );
    	$image_path = $cfg[ 'path' ][ 'relative' ] . '/img/ratings/';
    	
    	$rating = (float) $_GET[ 'rating' ];
    	$rating = round( ( $rating * 2 ), 0 ) / 2; // Round to nearest half
    	
    	if ( $rating < 1 || $rating > 5 ) {
    	
    		$rating = 'unrated';
    	
    	}
    	
    	
    	if ( ! $image = @ImageCreateFromPng( $image_path . $rating . '.png' ) ) {
    		
    		$width = 60;
    		$height = 12;
    		
    		$image  = imagecreatetruecolor( $width, $height );
    		$background = imagecolorallocate( $image, 255, 255, 255 );
    		imagecolortransparent( $image, $background );
    		$forground  = imagecolorallocate( $image, 0, 0, 0 );
    		imagefilledrectangle($image, 0, 0, $width, $height, $background );
    		imagestring( $image, 1, 3, 3, 'Error!', $forground );
    	
    	}
    	
    	header('Content-type: image/png');
    
    	imagepng( $image );
    	imagedestroy( $image );
    	
    ?>
    PHP:
    What's going on?
     
    Last edited: Aug 4, 2009
    Synchronium, Aug 4, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Add these two lines before imagepng($image);

    imagealphablending($image, true); 
    imagesavealpha($image, true); 
    
    PHP:
    (This is necessary to work with transparent pngs such as yours)
     
    premiumscripts, Aug 4, 2009 IP
    Synchronium likes this.
  3. Synchronium

    Synchronium Active Member

    Messages:
    463
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Fixed, thank you very much! :)
     
    Synchronium, Aug 4, 2009 IP