Home / Snippets / How to Easily Get Image Width and Height in PHP
How to Easily Get Image Width and Height in PHP cover

How to Easily Get Image Width and Height in PHP

431

2 years ago

0 comments

In this short snippet, you will learn how to easily get image width, height, and metadata in PHP. This can be achieved using the "getimagesize()" PHP built-in method.

getimagesize() method


This method accepts the image path and then will return an array of information. You can extract the value directly using the "list()" method and it's as follows.
<?php

list($width, $height, $type, $attr) = getimagesize("some-image.jpg");
The output of the above code will be like below.
[
    0 => 200,
    1 => 120,
    2 => 18,
    3 => 'width="200" height="120"',
    'bits' => 8,
    'mime' => 'image/webp',
]

getimagesize with Laravel Storage


Do note that you can also pass in the image path from the Laravel model like below. Imagine you have a Post model that store the path of the image that's located within the storage directory.
<?php

$path = storage_path('app/public/' . Post::first()->poster);

list($width, $height, $type, $attr) = getimagesize($path);
notion avatar

Ahmad Aziz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Post Tutorials

Learn trending tutorials for ReactJs, React Native, VueJS, NextJs, Laravel, Tailwind CSS, and Flutter development.
View Tutorials

Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your web projects
View Components

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this