I need to resize an image to a defined width and height but crop the bottom to the height if it is larger or add blank space to the bottom if it is smaller. How to accomplish that with the help of PHP's GD?
I maid a function for this: PHP/GD Imagestyle
You can create thumbnails exactly as you described with the following:
$thumb = imagestyle($image,'autosize:100 100');
But also if you need something more complicated you can use:
// resize 200 0 means width=200 height=auto
$thumb = imagestyle($image,'resize:200 0; crop:200 200;');
You can use phpThumb library.
Take a look https://github.com/masterexploder/PHPThumb/wiki/Basic-Usage
Related
I want to resize my through Intervention\Image in Laravel 5.3.
With the following code:
$resized = Image::make($image)->resize(640, null, function ($constraint) {
$constraint->aspectRatio(); })->stream();
$resized_thumb = Image::make($image)->resize(320, 213)->stream();
I get images like this:
http://clasifire.com/listing?category=1 ( If you click the first image to go to its details you will find that its height is actually more)
But with this images lose the aspect ratio.
What I want is, for example, consider this list on craigslist: https://sfbay.craigslist.org/d/antiques/search/ata, (scroll down 3-4 lines)
So I do not want images to be stretched, instead they should maintain aspect ratio, leaving blank space along width/height if required.
You should add another constraint:
$constraint->upsize();
after
$constraint->aspectRatio();
to prevent image from being scaled up if it's too small.
You may consider using a package like Glide to generate images on the fly (crop, size, effects, watermark, caching etc.) and keep the original image as is.
http://glide.thephpleague.com/
You should not specify with and height together! Only use one dimension and one of widen() OR heighten() functions :
By Height:
$resized_thumb = Image::make($image)->heighten(320)->stream();
By Width:
$resized_thumb = Image::make($image)->widen(213)->stream();
There are images displayed inside a HTML table in a PHP code :
The code of this table is :
for ($i = 0; $i < $data['list']['cnt']; $i++) {
$tabRows[$i][1]['width'] = "45%";
$tabRows[$i][1]['align'] = "center";
$tabRows[$i][1]['value'] = '<img src="'.HTTP_FIGURES.$data['list'][$i]['fig_image'].'" />';
}
As you can see the background of the images are seen and they make the page dirty. So I want to remove the background of each image. How to do that ?
Make the images a transparent PNG.
Your only practical solution is to go through each image with an image editor, delete the background and re-save as a .png with transparency.
Take any image editor like GIMP (free), Paint.net(free), Photoshop or any other image editor and add transparency where you need it. Here is a tutorial for paint.net. If your images are not in PNG - you will need to make PNG images as JPEG has no transparency.
You have 2 options:
Edit images with a image editor like paint.net and make the backgrounds transparent.
Use the PHP GD functions to 'edit' the images. This can be cumbersome, because you have to determine what color you want to replace with a background color. Often, a fixed color is used, or the color in pixel[1,1].
My advise, if there are not to many images, go for 1.
So here is my problem, I know the width of the images, but I don't know the height, and I'm using a preloader, my problem is that when the images are loading...the content under the images bounces down...when it's fully loaded, If I had fixed images width&height it will be easy to solve this, just set them using width and height attributes and it will look ok.
But is there a way to set the height before the page is rendered, maybe using PHP or so.
You can use getimagesize() on the server side with php to get image height and add that data properly to html. If you have too many images and you don't want to go though them every time the page loads, then consider caching this data in database somewhere
Well, you can get the hight and width via imagesx and imagesy then print it.
<?php
$img = 'image.jpg';
$image = imagecreatefromjpeg($img);
$width = imagesx($image);
$height = imagesy($image);
print '<img src="'.$img.'" height="'.$height.'" width="'.$width.'" alt="" />';
?>
I have a gif file with many icons and buttons on it..
What I want to do is include that gif file using php gd somefunction("file.gif")
then, resize the new image to 30px by 30px.. and then be able to position (using x and y coordinates) the actual gif file, so that only a certain area of the image shows on the new file..
just like the css background-position property but, with gd.
Thanx
imagecopyresized() resizes the image, so it doesn't quite work for what I was exactly looking for.
For anyone else that might need this, the best function is actually imagecopy().
http://www.php.net/manual/en/function.imagecopy.php
It will do the trick..
Thanx Trufa for the quick answer though.
imagecopyresized()
http://www.php.net/manual/en/function.imagecopyresized.php
Should do the trick for adjusting the size.
I have a web page that displays images that I don't know their size in advance. I was trying to use the GD functions to make the script resize and crop the images from me " Just before they are displayed.. I don't need caches" but I failed.
I need a script that I can call like this
<img src="display.php?src=blablabla&height=100&width=200" ?>
or even by calculating the width and height of css to preserve the proportions and make the image touch the box from inside like
<img src="blabla.jpg" style="height:<?php echo $height; ?>; width:<?php echo width; ?>" />
I don't need any sort of caching. How can I do that ?
WideImage rlz! :)
The resize's like that:
header('Content-type: image/jpeg');
echo WideImage::load('image.jpg')->resize(200, 100)->asString('jpg', 80);
// image.jpg resized at 200x100 with 80% of quality
You'll need to use the first style. Because this would be happening server-side, you can't check the CSS to get the desired size.
You just need to use the GD functions to open the appropriate file, use imagecopyresampled() to resize it, and then output to the buffer using imagejpeg. Don't forget to set the right headers:
header('Content-type: image/jpeg');
OR phpthumb http://phpthumb.sourceforge.net/
Demo is available at: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php
You are looking for TimThumb (Demo | Source Code):
Simply copy the source code into a new
document called ‘timthumb.php’, place
it in a folder on your site (ex:
/scripts/) and call the image like
this:
<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="">