1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need help with my Logic (Resize an image)

Discussion in 'HTML & Website Design' started by fullylucky, Mar 28, 2020.

  1. #1
    Hey Guys!!

    I'm trying to put some PHP code to resize an image.

    Basically users upload an image. And that image gets placed on a PDF document but I need to limit how big (Pixels) that image is. If it's height is too large it will push the contents on to the 2nd page. Which I don't want.


    Currently, I have this code which works to some extent but what I want is if the width of the picture is more than 80 that's totally ok as it doesn't push the page down.:

    But then again I wanna make sure the width doesn't exceed 480 pixels if so I wanna scale it down.

    Like I want to limit height to 80 and width to 480 if greater then scale in aspect ratio (without skewing it) down to these. :(

    Any help or advice would be greatly appreciated.


            function imageResize($imageResourceId,$width,$height) {
                $ratio = $width/$height; // width/height
                if( $ratio > 1) {
                    $targetWidth = 80;
                    $targetHeight = 80/$ratio;
                }else {
                    $targetWidth = 80*$ratio;
                    $targetHeight = 80;
                }
                $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
                imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
                return $targetLayer;
            }
    PHP:

     

    Attached Files:

    Solved! View solution.
    Last edited: Mar 28, 2020
    fullylucky, Mar 28, 2020 IP
  2. #2
    Try this:
    Don't supply width and height of image. Instead supply max width and max height that is allowed.
    $im is same as $imageResourceId


    function imageResize($im, $maxWidth=480, $maxHeight=80) {

    $width= imagesx($im);
    $height= imagesy($im);

    if( $width > $maxWidth ){
    $tw= $maxWidth;
    $th= round( ( $maxWidth * $height )/$width ,0);
    }elseif( $height > $maxHeight ){
    $th= $maxHeight;
    $tw= round( ($maxHeight*$width)/$height ,0);
    }else{
    $tw= $width;
    $th= $height;
    }

    $targetLayer=imagecreatetruecolor($tw, $th);
    imagecopyresampled($targetLayer, $im,0,0,0,0, $tw,$th, $width,$height);
    return $targetLayer;
    }
     
    JEET, Mar 28, 2020 IP
  3. fullylucky

    fullylucky Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #3
    I changed it to this. Im testing it now to see if it works or not :(

            function imageResize1($imageResourceId,$width,$height) {
                $ratio = $width/$height; // width/height
                $widthlimit = 480;
                $heightlimit = 80;
                $targetratio = $widthlimit/$heightlimit;
                if( $ratio > $targetratio ) {
                    $targetWidth = $widthlimit;
                    $targetHeight = $widthlimit/$ratio;
                }else {
                    $targetWidth = $heightlimit*$ratio;
                    $targetHeight = $heightlimit;
                }
                $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
                imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
                return $targetLayer;
            }
    PHP:
     
    Last edited: Mar 29, 2020
    fullylucky, Mar 28, 2020 IP
  4. fullylucky

    fullylucky Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #4
    THANKS for your suggestion about imagesx function. I didn't know about it.

    ahh my code didn't work. I'll try your way.
     
    fullylucky, Mar 28, 2020 IP
  5. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #5
    With the new function you posted, your images will skew badly.

    Did you tried the function I posted?
     
    JEET, Mar 28, 2020 IP
  6. fullylucky

    fullylucky Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #6
    I updated this

    it was like this:
    $ratio < $targetratio

    now it's this
    $ratio > $targetratio

    it's working now.

    I'll give your code a try. it should also work. Im still getting my head around it.
     
    fullylucky, Mar 29, 2020 IP
    JEET likes this.
  7. fullylucky

    fullylucky Well-Known Member

    Messages:
    40
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    108
    #7
    I learnt something else more important... actually don't need to limit this at all but it does help save on space when users upload images.

    But I learnt that at the time of inserting the image you can do this:

    <img src="path" style="max-height: 80px; max-width: 480px;" />

    I researched online and it said it does keep the aspect ratio. and limits the image which is what I needed.
     
    fullylucky, Mar 29, 2020 IP
    JEET likes this.
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    The big problem here with your logic is you're trying to use ONE ratio instead of scaling, with an ELSE when it should be "and" style logic where BOTH checks for both directions needs be made.

    
    
    // note, round off, there's no such thing as fractional pixels
    
    $targetWidth = $width;
    $targetHeight = $height;
    
    if ($targetWidth > 480) {
    	$ratio = 480 / $targetWidth;
    	$targetWidth = round($targetWidth * $ratio);
    	$targetHeight = round($targetHeight * $ratio);
    }
    if ($targetHeight > 80) {
    	$ratio = 80 / $targetHeight;
    	$targetWidth = round($targetWidth * $ratio);
    	$targetHeight = round($targetHeight * $ratio);
    }
    Code (markup):
    Is the logic you want when setting max-width and max-height whilst preserving aspect ratio. Check one direction, scale to that, THEN check the other direction and scale to that. Right now the way your logic is working if the result of the width correction is over the height correction, the height never gets done!

    ... and yes, you can limit the size client-side, but don't send massive images client-side if it can be avoided. Though with retina displays and varying dpi, 2x the target resolution is increasingly common. Take a look at the <picture> tag to see how you can now declare multiple images in multiple sizes so you're only using the bandwidth you NEED to use on your pages.
     
    deathshadow, Mar 29, 2020 IP
    JEET likes this.