tyra Posted February 5, 2011 Share Posted February 5, 2011 // 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 but white is needed. what to add and where ? Link to comment https://forums.phpfreaks.com/topic/226752-thumbnail-backround-setting/ Share on other sites More sharing options...
litebearer Posted February 5, 2011 Share Posted February 5, 2011 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 https://forums.phpfreaks.com/topic/226752-thumbnail-backround-setting/#findComment-1170141 Share on other sites More sharing options...
tyra Posted February 5, 2011 Author Share Posted February 5, 2011 doesn't work for me because $result = imagecopy($targetImage, $sourceImage, 0, 0, $X, $Y, $W, $H); runs it over with black again. Link to comment https://forums.phpfreaks.com/topic/226752-thumbnail-backround-setting/#findComment-1170160 Share on other sites More sharing options...
harristweed Posted February 5, 2011 Share Posted February 5, 2011 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 https://forums.phpfreaks.com/topic/226752-thumbnail-backround-setting/#findComment-1170186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.