html - Display an image to be a certain size in PHP
Get the solution ↓↓↓Solution:
using both fixed height and width will stretch the image. Use only one height or width in img tag or use css height or width to zoom image to fixed dimension.
1.) use either height or width in img tag in html 2.) use css either height or with 3.) resize image proportionately using image resize in php. for image resize you can use any 3rd party class. eg. suggested above.
Answer
Solution:
I suggest resizing all images as they are uploaded and store the resized image along with the original.
Answer
Solution:
You may use a script like this (just an example code).
I recommend you to implement an image cache for resized files.
<?php
$image = "images/worldsmap_bg.png";
if(!file_exists($image))
throw new Exception("file doesn't exists");
$imageinfo = getimagesize($image);
$maxwidth = 120;
$maxheight = 150;
$origratio = $imageinfo[0]/$imageinfo[1];
$width = $imageinfo[0];
$height = $imageinfo[1];
if($height>$maxheight)
{
$faktor = $maxheight/$imageinfo[1];
if($faktor < 1)
{
$width = round($width*$faktor);
$height = round($height*$faktor);
}
}
if($width>$maxwidth)
{
$faktor = $maxwidth/$width;
if($faktor < 1)
{
$width = round($width*$faktor);
$height = round($height*$faktor);
}
}
$image_p = imagecreatetruecolor($width, $height);
switch($imageinfo[2]):
case IMAGETYPE_GIF:
$image = imagecreatefromgif($image);
break;
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
$image = imagecreatefromjpeg($image);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($image);
break;
default:
throw new Exception ("invalid image");
break;
endswitch;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $imageinfo[0], $imageinfo[1]);
header("Content-type: " . image_type_to_mime_type($imageinfo[2]));
switch($imageinfo[2]):
case IMAGETYPE_GIF:
imagegif($image_p);
break;
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
imagejpeg($image_p);
break;
case IMAGETYPE_PNG:
imagepng($image_p);
break;
default:
throw new Exception ("invalid image");
break;
endswitch;
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.