Changing arbitrary image size in PHP

What would be the best way to resize images, which can be any size, to a fixed size, or at least for a fixed size?

Images come from random URLs that are not under my control, and I have to ensure that the images do not go out from around 250 pixels x 300 pixels or 20% to 50% of the layer.

I would think that I first determine the size, and if it falls outside the range, change its size, but I'm not sure how to work out the logic for resizing if the image size can be anything.

edit: I do not have local access to the image, and the image URL in variable c is displayed using img src = ..., I need to specify the values ​​of the width and height tags.

+2
source share
6 answers

This is easy to do with ImageMagick. Either you use the conversion command line tool through exec, or you use http://www.francodacosta.com/phmagick/resizing-images .
If you use β€œconvert”, you can even say that ImageMagick simply resizes large images and does not convert smaller ones: http://www.imagemagick.org/Usage/resize/

If you do not have local access, you cannot use ImageMagick:

<?php $maxWidth = 250; $maxHeight = 500; $size = getimagesize($url); if ($size) { $imageWidth = $size[0]; $imageHeight = $size[1]; $wRatio = $imageWidth / $maxWidth; $hRatio = $imageHeight / $maxHeight; $maxRatio = max($wRatio, $hRatio); if ($maxRatio > 1) { $outputWidth = $imageWidth / $maxRatio; $outputHeight = $imageHeight / $maxRatio; } else { $outputWidth = $imageWidth; $outputHeight = $imageHeight; } } ?> 
+4
source

Have you looked at the GD library documentation, in particular the imagecopyresized method?

+1
source

If you need to keep the aspect ratio of the image intact, you must scale it by a factor so that the longer side of the image (height or width) matches the restrictions for that side.

+1
source

You can use the following:

 $maxWidth = 250; $maxHeight = 500; $validSize = true; $imageinfo = getimagesize($filename); if ($imageinfo) { $validSize &= $imageinfo[0] <= $maxWidth; $validSize &= $imageinfo[1] <= $maxHeight; } 

&= is a bitwise combined operator and the & operator and an assignment operator = . So $foo &= *expr* and $foo = $foo & *expr* equivalent.

0
source

I am not sure about the exact solution to your answer, but I know a project written in PHP that has a solution. Go and take a look at ImageCache, created for Drupal CMS, which is written in PHP.

Basically, you can define some actions that you need to take for an arbitrary image, and do almost any scaling / cropping that you want for the image.

You will need to learn some of the Drupal APIs to understand this example, but it is extensive, and if you understand their algorithms, you can solve other more complex problems.

0
source

I created the following function for intelligently resizing images in relation to the ratio, and even has a parameter for scaling smaller images (which is crucial if your HTML layout is screwed when thumbnails are simpler).

 function ImageIntelligentResize( $imagePath, $maxWidth, $maxHeight, $alwaysUpscale ) { // garbage in, garbage out if ( IsNullOrEmpty($imagePath) || !is_file($imagePath) || IsNullOrEmpty($maxWidth) || IsNullOrEmpty($maxHeight) ) { return array("width"=>"", "height"=>""); } // if our thumbnail size is too big, adjust it via HTML $size = getimagesize($imagePath); $origWidth = $size[0]; $origHeight = $size[1]; // Check if the image we're grabbing is larger than the max width or height or if we always want it resized if ( $alwaysUpscale || $origWidth > $maxWidth || $origHeight > $maxHeight ) { // it is so let resize the image intelligently // check if our image is landscape or portrait if ( $origWidth > $origHeight ) { // target image is landscape/wide (ex: 4x3) $newWidth = $maxWidth; $ratio = $maxWidth / $origWidth; $newHeight = floor($origHeight * $ratio); // make sure the image wasn't heigher than expected if ($newHeight > $maxHeight) { // it is so limit by the height $newHeight = $maxHeight; $ratio = $maxHeight / $origHeight; $newWidth = floor($origWidth * $ratio); } } else { // target image is portrait/tall (ex: 3x4) $newHeight = $maxHeight; $ratio = $maxHeight / $origHeight; $newWidth = floor($origWidth * $ratio); // make sure the image wasn't wider than expected if ($newWidth > $maxWidth) { // it is so limit by the width $newWidth = $maxWidth; $ratio = $maxWidth / $origWidth; $newHeight = floor($origHeight * $ratio); } } } // it not, so just use the current height and width else { $newWidth = $origWidth; $newHeight = $origHeight; } return array("width"=>$newWidth, "height"=>$newHeight); } 
0
source

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


All Articles