Jump to content

thumbnail backround setting


tyra

Recommended Posts

 // Create the target image
        if ( function_exists('imagecreatetruecolor') ) {
            
		$targetImage = imagecreate($width, $height);

        } else {
            $targetImage = imagecreate($width, $height);

        }
        if ( ! is_resource($targetImage) ) {
            user_error('Cannot initialize new GD image stream', E_USER_NOTICE);
            return false;
        }

        // Copy the source image to the target image
        if ( $options['method'] == 2 ) {
            $result = imagecopy($targetImage, $sourceImage, 0, 0, $X, $Y, $W, $H);
		//imagejpeg($targetImage, $savePath . ( $filename = uniqid() . time() . '.jpg' ) );
        } elseif ( function_exists('imagecopyresampled') ) {
            $result = imagecopyresampled($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
		//imagejpeg($targetImage, $savePath . ( $filename = uniqid() . time() . '.jpg' ) );
        } else {
            $result = imagecopyresized($targetImage, $sourceImage, 0, 0, $X, $Y, $width, $height, $W, $H);
		//imagejpeg($targetImage, $savePath . ( $filename = uniqid() . time() . '.jpg' ) );
        }
        if ( ! $result ) {
            user_error('Cannot resize image', E_USER_NOTICE);
            return false;
        }

        // Free a memory from the source image
        imagedestroy($sourceImage);

        // Save the resulting thumbnail
        return $targetImage;
    }

creates thumbnail with black backround like this

LB4Ny.png

but white is needed.

what to add and where ?

Link to comment
Share on other sites

A clue...

/* create an image */
$im = imagecreatetruecolor(400, 30);

/* define some colors */
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

/* fill the image with a color */
imagefilledrectangle($im, 0, 0, 399, 29, $white);

Link to comment
Share on other sites

I have done something similar in the past, here is my code, you should be able to amend it to suit....

$size = GetImageSize ("$path_to_image");   // set image name here
    $old_width = $size[0];
    $old_height = $size[1];

    if($old_width/$old_height < 1.33334){//is it landscape or portrate?
        $new_height=300;
        $new_width=round($old_width/$old_height*300);
    }else{
    $new_width = $width;
    $new_height = round($old_height * $width / $old_width);  
    }

    $source_path=$where_temp;   //Source File path
    $destimg=imagecreatetruecolor($new_width,$new_height) or die("<p>Problem In Creating image</p>");

    $srcimg=ImageCreateFromJPEG($source_path.$image_name) or die("<p>Problem In opening Source Image</p>");
    // imagecopyresampled for better quality
    imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die("<p>Problem In resizing</p>");
    //create an image 400x300
    $base=imagecreatetruecolor(400,300);
    
    $white= imagecolorallocate($base,208,244,255);
    //make white background
    imagefill($base,0,0, $white);
    //calculate the starting x position to place image in center of background
    $xpos=round((400-$new_width)/2);
    //merge the two images
    imagecopy($base,$destimg,$xpos,0,0,0,$new_width,$new_height);
    
    ImageJPEG($base,$destination_path.$image_name) or die("<p>Problem In saving</p>");
}

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.