raduenea Posted June 6, 2012 Share Posted June 6, 2012 I have a php function that resize an image: function resize_image($max_width,$max_height){ $width = $this->getWidth($this->image_location); $height = $this->getHeight($this->image_location); $new_width = ""; $new_height = ""; $with_scale = $width/$max_width; $height_scale = $height/$max_height; if($with_scale > $height_scale){ $new_width = $max_width; $new_height = ($max_width/$width) * $height; }else{ $new_height = $max_height; $new_width = ($max_height/$height) * $width; } $newImage = imagecreatetruecolor($new_width,$new_height); $source = imagecreatefromjpeg($this->image_location); imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height); imagejpeg($newImage,$this->new_location,85); chmod($this->new_location, 0777); return $this->new_location; } If I use a picture that have 500x300 , and after re-size I have something 200x100 , I don't want to save it with this dimensions. I want to put this picture with 200x100 over a white square 200x200 vertical align. How can I change this code for this ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/ Share on other sites More sharing options...
smoseley Posted June 6, 2012 Share Posted June 6, 2012 resize_image_and_pad(200,200); function resize_image_and_pad($width,$height){ $img_width = $this->getWidth($this->image_location); $img_height = $this->getHeight($this->image_location); $img_scale = $img_width/$img_height; $new_width = null; $new_height = null; $scale = $width/$height; if($scale < $img_scale){ $new_width = $width; $new_height = $new_width * $img_scale; }else{ $new_height = $height; $new_width = $new_height * $img_scale; } $x = ($width - $new_width) / 2; $y = ($height - $new_height) / 2; $newImage = imagecreatetruecolor($width,$height); $source = imagecreatefromjpeg($this->image_location); imagecopyresampled($newImage,$source,$x,$y,0,0,$new_width,$new_height,$new_width,$new_height); imagejpeg($newImage,$this->new_location,85); chmod($this->new_location, 0777); return $this->new_location; } Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1351610 Share on other sites More sharing options...
raduenea Posted June 6, 2012 Author Share Posted June 6, 2012 The code that you change it doesn't return me anything. I believe that I need a white picture with 200x200, and over to put the new 200x120 and save as one picture. How can I do that in code ? Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1351700 Share on other sites More sharing options...
Barand Posted June 6, 2012 Share Posted June 6, 2012 when you do your imagecopy, make the 200x200 white square your destination image and place the copy image 40 pix from the top ( (200 - 120) / 2 ) Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1351706 Share on other sites More sharing options...
smoseley Posted June 7, 2012 Share Posted June 7, 2012 The code that you change it doesn't return me anything. I believe that I need a white picture with 200x200, and over to put the new 200x120 and save as one picture. How can I do that in code ? My code does exactly what you want it to. It may have some bugs, but it is correct. Debug it and be happy. Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1351812 Share on other sites More sharing options...
raduenea Posted June 7, 2012 Author Share Posted June 7, 2012 I figure it out. here is the final code. function resize_image($max_width,$max_height){ $width = $this->getWidth($this->image_location); $height = $this->getHeight($this->image_location); $new_width = ""; $new_height = ""; $with_scale = $width/$max_width; $height_scale = $height/$max_height; if($with_scale > $height_scale){ $new_width = $max_width; $new_height = ($max_width/$width) * $height; }else{ $new_height = $max_height; $new_width = ($max_height/$height) * $width; } $x_mid = $new_width / 2; $y_mid = $new_height / 2; $newImage = imagecreatetruecolor($new_width,$new_height); $source = imagecreatefromjpeg($this->image_location); imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height); $final = imagecreatetruecolor($max_width, $max_height); imagecopyresampled($final, $newImage, 0, 0, ($x_mid - ($max_width / 2)), ($y_mid - ($max_height / 2)), $max_width, $max_height, $max_width, $max_height); $bg_color = imagecolorallocate ($final, 255, 255, 255); imagefill($final, 0, 0, $bg_color); imagejpeg($final,$this->new_location,85); chmod($this->new_location, 0777); return $this->new_location; } Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1351879 Share on other sites More sharing options...
smoseley Posted June 7, 2012 Share Posted June 7, 2012 Why are you creating 2 new images? Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1351955 Share on other sites More sharing options...
raduenea Posted June 8, 2012 Author Share Posted June 8, 2012 How this can be change using only 1 image ? Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1352094 Share on other sites More sharing options...
smoseley Posted June 8, 2012 Share Posted June 8, 2012 I showed you how in my reply. I actually spent quite a bit of time. Why not try to read my reply and understand what I'm doing in it? Quote Link to comment https://forums.phpfreaks.com/topic/263753-resize-image-white-background/#findComment-1352125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.