Resize Image Using Aspect Ratio

As a brief run, I am now creating a dating-type site. Users can create accounts and upload profile photos (up to 8). To display them in the viewing area of ​​the website, I am looking for a way in PHP (with a third-party processor / scripts) to resize all downloaded images to have thumbnails that adhere to certain sizes.

As an example, I want the "profile" images (thumbnails) to be NO greater than 120 * 150 pixels. To create scenarios, you need to resize the downloaded images (regardless of whether they are portrait or landscape and regardless of the proportions) to adhere to these measurements without stretching.

The width (for example, 120 pixels) should always remain unchanged, but the height (for example, 150 pixels) can be changed to maintain the image proportionally. If this is a landscape photograph, I assume that the script will need to take out a piece from the middle of the image?

The reason all the images to be resized is because when the profiles are displayed in the grid, all the thumbnails are about the same size.

Any input is welcome.

+3
source share
5 answers
$maxwidth = 120;
$maxheight = 150;

$img = imagecreatefromjpeg($jpgimage); 
//or imagecreatefrompng,imagecreatefromgif,etc. depending on user uploaded file extension

$width = imagesx($img); //get width and height of original image
$height = imagesy($img);

//determine which side is the longest to use in calculating length of the shorter side, since the longest will be the max size for whichever side is longest.    
if ($height > $width) 
{   
$ratio = $maxheight / $height;  
$newheight = $maxheight;
$newwidth = $width * $ratio; 
}
else 
{
$ratio = $maxwidth / $width;   
$newwidth = $maxwidth;  
$newheight = $height * $ratio;   
}

//create new image resource to hold the resized image
$newimg = imagecreatetruecolor($newwidth,$newheight); 

$palsize = ImageColorsTotal($img);  //Get palette size for original image
for ($i = 0; $i < $palsize; $i++) //Assign color palette to new image
{ 
$colors = ImageColorsForIndex($img, $i);   
ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
} 

//copy original image into new image at new size.
imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($newimg,$outputfile); //$output file is the path/filename where you wish to save the file.  
//Have to figure that one out yourself using whatever rules you want.  Can use imagegif() or imagepng() or whatever.

, , ( ), . , , , , . , 200x300 100x150, 300x200 120x80.

, , 120, , , - , 200x300, 120x180 - , , .

Letterboxing , x y, , imagecopyresized(). - 100x150, , 10, 10px 120x150. Letterboxing 120x80 X 0, Y 35, 120x150 35px .

$newimg $maxwidth, $maxheight, $newwidth, $newheight, imagecopyresized() .

, :

if ($height > $width) 
{   
$ratio = $maxheight / $height;  
$newheight = $maxheight;
$newwidth = $width * $ratio; 
$writex = round(($maxwidth - $newwidth) / 2);
$writey = 0;
{
else 
{
$ratio = $maxwidth / $width;   
$newwidth = $maxwidth;  
$newheight = $height * $ratio;   
$writex = 0;
$writey = round(($maxheight - $newheight) / 2);
}

$newimg = imagecreatetruecolor($maxwidth,$maxheight);

//Since you probably will want to set a color for the letter box do this
//Assign a color for the letterbox to the new image, 
//since this is the first call, for imagecolorallocate, it will set the background color
//in this case, black rgb(0,0,0)
imagecolorallocate($newimg,0,0,0);

//Loop Palette assignment stuff here

imagecopyresized($newimg, $img, $writex, $writey, 0, 0, $newwidth, $newheight, $width, $height);

, .

+10

GD Imagick - , , PHP.
, , :(

+2

( ) php Zebra_Image, , PHP. , . . , .

, php.ini , (, 2560x1600). , . imagecreatefrom{gif,jpeg,png} function _create_from_source 1262, 1279 1288. @, , . @ PHP, . 32 , 64 . 2560x1600, , .

.

    $image_properties = getimagesize($UPLOADED_FILE_PATH);                   
    $file_width = $image_properties[0];                                
    $file_height = $image_properties[1];                               

    if ($file_width > 2560 || $file_height > 1600) {    
        // handle your error whichever you like, I simply 'die' just to show               
        die('Cannot manipulate image bigger than 2560x1600');                                                                                                                                              
    }       

(: Zebra Image 2.2.2)

+1

ImageMagick:

convert susan.jpg -resize x200 susan_thumb.jpg

, PHP shell_exec() . , PHP.

ImageMagick . , x200 " 200 ".

ImageMagick ( ghostscript): , , , PDF ImageMagick, Ghostscript, Windows Vista/7 x64 , , .

- GD ( dqhendricks). , -, , .

0

. , , PHP GD . letterboxing crop-to-fit . . .

http://www.spotlesswebdesign.com/blog.php?id=1

, , . !

0

Source: https://habr.com/ru/post/1783337/


All Articles