Hi all
I want to display image uploaded from a form in a div size of "width=235px and height=61px" with image good quality also(i mean not shrinked or stretched) using php. I'm uploading a image of size "width=452px and height=507px". I have tried the codes i found on internet but all the codes are reducing the image either weight wise or height wise. I want my uploded image to be displayed in width = 235px and hight = 61px with quality.

I tried this type of code but this is not reducing my image to 23561 it is reducing the image to 8161

`<?php
function createThumbnail($img, $imgPath, $suffix,$quality)
{

list($width, $height, $image_type) = getimagesize($img);
$w=235;
$h=61;

$r = $width / $height;
if ($crop) {
    if ($width > $height) {
        $width = ceil($width-($width*($r-$w/$h)));
    } else {
        $height = ceil($height-($height*($r-$w/$h)));
    }
    $newwidth = $w;
    $newheight = $h;
} else {
    if ($w/$h > $r) {
        $newwidth = $h*$r;
        $newheight = $h;
    } else {
        $newheight = $w/$r;
        $newwidth = $w;
    }
}

$src = imagecreatefromjpeg($img);

$tmp = imagecreatetruecolor($newwidth, $newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth, $newheight,$width,$height); 

// Create the new file name.
$newNameE = explode(".", $img);
$newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .'';

// Save the image.
imagejpeg($tmp, "$imgPath/$newName", $quality) or die("Cant save image");
echo "<img src='$imgPath/$newName' style='max-width:100; max-height:100;'>";
// Clean up.
//imagedestroy($src);
imagedestroy($tmp);
return true;
}

$thumb = createThumbnail("hotel-view.jpg", "images", "-thumb", 100);

?>`

Can some body please help me with this issue.It need to be fixed very urgently.

The image you are uploading (452x507), and the output you want (235x61) have different aspect ratios, that's why that code fails. How do you want to show that portrait image in a smaller landscape version ? Do you want to resize and then crop a part?

No i don't want to crop the image i want the image to be scaled and display the entire image in a
required dimensions without loosing the quality

You can scale the image based on the proportional ratio of your maximum width and the actual height. However, if we properly scale 452x507 to your maximum dimension. The properly scaled output will only be around 54.21px by 60.84 px.

Here are the reasons why?

To scale we need to check if the actual image is a wide image or tall image. To find out if the image is either one, we need to put the actual image dimension into simple calculation.

let x = width, and y = height.

Image is wide if(x > y), and image is tall if(y > x).

If the image is tall, then we can properly scale the image by calculating the slope or in this case the quotient of the maximum height vs. the actual height of the image. Therefore, in our case the quotient can be presented by this simple formula.

let max_y = 61px, and max_x = 235px

calculate quotient => mq = (max_y) / (y) => (61px)/ (507px) = 0.12

calculate the scaled width => final width => x * mq => 452px * 0.120 = 54.24px for the output width

calculate the scaled height => final height-> y * mq => 507px * 0.120 = 60.84 px or round it 60px.

If you want me to translate the above calculation in php script to work with images , please let me know..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.