Get images height, before they are loaded - php

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="" />';
?>

Related

How to convert <img> to AMP in PHP

I have problem when covert old html code to AMP version:
My document with many image, but only have width value in source code.
<img src="/url-img1.jpg" with="728"></img>
<img src="/url-img2.jpg" with="640"></img>
In AMP version i need add height value as below:
<amp-img src="/url-img1.jpg" width="{widht-of-image}" height="{height-of-image}" layout="responsive"></amp-img>
<amp-img src="/url-img2.jpg" width="{widht-of-image}" height="{height-of-image}" layout="responsive"></amp-img>
Can anyone pls tell me how to do it?
You can use getimagesize()
list($width, $height) = getimagesize('path_to_the_image/url-img1.jpg');
echo '<amp-img src="/url-img1.jpg" width="'. $width .'" height="'. $height . '" layout="responsive"></amp-img>';
However, this defeats a bit the purpose of AMP which is that the resources are loaded at the end. Caching on their side should solve these issues, but still my advise is that you are hardcoding the width, you also hardcode the height. You could automate this once and then replace them for all.

How to resize an image as follows?

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

Dynamic images resizing

I'm working on project where we are trying to adopt and resize template images to the various resolutions. For example if the website is viewed in 800px width (800x600) and 1024px width or larger the image size should be viewed in same quality.
I've had in mind to use sprite with 3 types of images for each range of this template , but I'm looking for other ideas , php gd maybe ? Any python solution ?
Well, for resizing it would of course be better to use GD... But indexed, I think. So that you have an upload script that automatically generates the images' in other sizes, and saves them somewhere.
However, it matters whether you have more disk space, or performance... Performance would get worse IF you have many people viewing these images. Disk space would get worse IF you have A LOT of these images.
Python Imaging Library will give you dynamic resizing, processing, etc.
If you are resizing to a known set of resolutions, you can just resize your images once and store them.
If you need to resize for any possible resolution, you will need a library to do that for you. In PHP, GD or ImageMagik are both good.
If you do this, you may want to add caching for the most common resolutions. This will take up more disc space, but will save you the cost of recalculating all the images every time.
Note that it can be difficult to detect the true resolution though. If the browser window is resized, the resolution you think the screen is may not be the actual resolution the user can see. The same can happen if they have toolbars or sidebars opened.
Why not resize the image on the client using JavaScript?
<head>
<script>
function resize() {
ww = window.innerWidth
wh = window.innerHeight
photo = document.getElementById("photo")
// You probably wouldn't actually make the image fill the window, you'd pick
// some appropriate size.
photo.setAttribute("width", ww)
photo.setAttribute("height", wh)
}
</head>
<body onload="resize()" onresize="resize()">
<img id="photo" src="photo.jpg">
Getting the inner window width is quite hard, as different browsers use different variables. However, this is what I use on my website. It gets the inner window width rather reliably, and then sets the image width/height. It shouldn't be too hard to modify this code to set the src of the image desired.
function set_image_sizes(){
if (window.innerHeight != undefined) {
height = window.innerHeight;
width = window.innerWidth;
} else if (document.documentElement.clientHeight > 0) {
height = document.documentElement.clientHeight;
width = document.documentElement.clientWidth;
} else {
height = document.body.clientHeight;
width = document.body.clientWidth;
}
$('#image').css('height', height);
$('#image').css('width', width);
}

PHP crop and resize image on the fly

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="">

Rounded corners on images using PHP?

Does anyone know how to make a image have rounded corners using a PHP script?
It can be done using php-gd, but I ended up passing that task to the browser, using CSS:
<img src="photo.jpg" width="42"
height="42" alt="My cool photo"
style="border-radius: 15px;
-moz-border-radius: 15px;" />
Download easyphpthumbnail.class.php from this link
from this you can resize and convert image into rounded image.
in below example image is converted into transparent circle image.
include_once('easyphpthumbnail.class.php');
$source = 'demo.jpg';
$width = 100;
$height = 100;
$thumb = new easyphpthumbnail;
$thumb -> Thumbheight = $width;
$thumb -> Thumbwidth = $height;
$thumb -> Backgroundcolor = '#FFFFFF';
$thumb -> Clipcorner = array(2,50,0,1,1,1,1);
$thumb -> Maketransparent = array(1,0,'#FFFFFF',10);
$thumb -> Createthumb($source);
You can look at https://www.phpcontext.com/thumbnailer/ . There's a script for creating nice rounded corner thumbs with PHP. They are antialiased too.
Instead of modifying the image, why not just wrap it in some HTML that has images at each corner that overlay the original to provide the appearance of rounded corners?
By doing the image editing in your .php script, you're going to put undue load on your web server, and that means your application won't scale well.
GD is great for image manipulation, but it would be much easier for you and much easier on your server if you used CSS.
Here's a great tutorial for some cool image effects using CSS:
http://www.webdesignerwall.com/tutorials/css-decorative-gallery/
For modern browsers, you can do it in pure CSS:
http://www.css3.info/preview/rounded-border/
A couple of other noteworthy ones:
http://www.spiffycorners.com/
http://www.html.it/articoli/niftycube/index.html
its easy to create some rounded thumbs using php, just use Thumbnailer :)
There are a lot of options available, you can find them using Google. The easiest way though is using the Thumbnailer. It's as simple as two lines of code:
// make an object
$th=new Thumbnailer("your-photo.jpg");
// create a 120x90 thumb and round its corners
$th->thumbFixed(120,90)->round()->save("your-thumb.jpg");
Fun it is, isn't it? :) There are a lot of other options available. The corners will be antialiased.
It seems most or all of the libraries referenced in the other answers here are now dead and gone for one reason or other.
After some exploring, I settled on claviska/SimpleImage as a good library for rounded rectangles (and lots of other handy stuff!)

Categories