{"id":17075,"date":"2024-05-17T06:00:00","date_gmt":"2024-05-17T06:00:00","guid":{"rendered":"https:\/\/www.directimpactsolutions.com\/?p=17075"},"modified":"2025-05-01T02:46:59","modified_gmt":"2025-05-01T02:46:59","slug":"resize-an-image-in-php","status":"publish","type":"post","link":"https:\/\/www.directimpactsolutions.com\/en\/resize-an-image-in-php\/","title":{"rendered":"How to Resize an Image in PHP"},"content":{"rendered":"<div class=\"wp-block-uagb-image uagb-block-72dfaf4c wp-block-uagb-image--layout-default wp-block-uagb-image--effect-static wp-block-uagb-image--align-none\"><figure class=\"wp-block-uagb-image__figure\"><img decoding=\"async\"  sizes=\"auto, (max-width: 480px) 150px\" src=\"https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-1024x576.jpg\" alt=\"\" width=\"600\" height=\"576\" title=\"\" loading=\"lazy\" role=\"img\"\/><\/figure><\/div><div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div><p>When building a website in <a href=\"https:\/\/www.directimpactsolutions.com\/en\/laravel\/\">Laravel<\/a> (or any other PHP-based framework) where users will be uploading images, you will probably use a library to manipulate and process those images. With cell phone cameras capturing larger, higher-resolution images, raw images that are uploaded could quickly fill the available hard drive space and cause performance issues on the website.<\/p><p>While PHP has image processing libraries like GD and <a href=\"https:\/\/github.com\/Imagick\/imagick\" target=\"_blank\" rel=\"noreferrer noopener\">Imagick<\/a>, these aren\u2019t always user friendly. There are a number of third-party libraries that make image processing easier.<\/p><p>In this article we\u2019ll be using <a href=\"https:\/\/github.com\/spatie\/image\" target=\"_blank\" rel=\"noreferrer noopener\">Spatie Image<\/a>, a PHP image processing library developed by <a href=\"https:\/\/spatie.be\/open-source\" target=\"_blank\" rel=\"noreferrer noopener\">Spatie<\/a>, who creates a number of open source libraries for Laravel and PHP. It provides an easy-to-use interface for common image manipulations like resizing, cropping, and adding effects.<\/p><div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><nav><ul><li class=\"\"><a href=\"#width-and-height\">Width and Height<\/a><\/li><li class=\"\"><a href=\"#resize\">Resize<\/a><\/li><li class=\"\"><a href=\"#fit\">Fit<\/a><\/li><li class=\"\"><a href=\"#crop\">Crop<\/a><\/li><li class=\"\"><a href=\"#adjustments\">Adjustments<\/a><\/li><li class=\"\"><a href=\"#effects\">Effects<\/a><\/li><li class=\"\"><a href=\"#watermarks\">Watermarks<\/a><\/li><li class=\"\"><a href=\"#text\">Text<\/a><\/li><\/ul><\/nav><\/div><div class=\"wp-block-uagb-advanced-heading uagb-block-a98b2fa2\"><h2 class=\"uagb-heading-text\">Installation and Basic Usage<\/h2><\/div><p><\/p><p>Installation is done with a fairly simple composer command:<\/p><pre class=\"wp-block-code\"><code>composer require Spatie Image<\/code><\/pre><p>You can then use the <code>Image<\/code> class to load and manipulate images.<\/p><pre class=\"wp-block-code\"><code>use Spatie\\Image\\Image;\n\n$image = Image::load('example.jpg)\n  -&gt;width(50)\n  -&gt;save();<\/code><\/pre><p>Calling <code>save<\/code> without any parameters will overwrite the existing file. If you want to save to a new file, then you would pass the new file path to the <code>save<\/code> method. You can save to a different file format by changing the extension.<\/p><pre class=\"wp-block-code\"><code>use Spatie\\Image\\Image;\n\n$image = Image::load('example.jpg)\n  -&gt;save('example.png');<\/code><\/pre><div class=\"wp-block-uagb-advanced-heading uagb-block-79d83f77\"><h2 class=\"uagb-heading-text\">Resize an Image in PHP<\/h2><\/div><p><\/p><p>Spatie Image provides a few different ways to resize an image.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-75b81a75de5692b121d24936d1976d9b\" id=\"width-and-height\">Width and Height<\/h3><p>The <code>width<\/code> and <code>height<\/code> functions take the desired dimensions in pixels as parameters and resize the image accordingly. The resized image will be contained within the given <code>$width<\/code> and <code>$height<\/code> dimensions respecting the original aspect ratio.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.png')\n    -&gt;width(300)\n    -&gt;height(250)\n    -&gt;save();<\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-1d17f0fefb1df4c6b8f70ff39a6d43ab\" id=\"resize\">Resize<\/h3><p>The resize function acts as a shortcut, allowing you to set width and height with a single method.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.png')\n    -&gt;resize(300, 250)\n    -&gt;save();<\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-aa048682f9097b501f477b21a8b22e62\" id=\"fit\">Fit<\/h3><p>The Fit function lets you resize the image to a given width and height, but also accepts a Fit function as a parameter that gives you a bit more control as to how the image is resized.<\/p><ul class=\"wp-block-list\"><li><code>Fit::Contain<\/code> resizes the image to fit within the width and height boundaries without cropping, distorting, or altering the aspect ratio.<\/li>\n\n<li><code>Fit::Max<\/code> resizes the image to fit within the width and height boundaries without cropping, distorting, or altering the aspect ratio, and will not increase the size of the image if it is smaller than the output size.<\/li>\n\n<li><code>Fit::Fill<\/code> resizes the image to fit within the width and height boundaries without cropping or distorting the image, and fills the remaining space with the background color (set using the <code>background<\/code> function). The resulting image will match the constraining dimensions.<\/li>\n\n<li><code>Fit::Stretch<\/code> stretches the image to fit the constraining dimensions exactly. The resulting image will fill the dimensions, and will not maintain the aspect ratio of the input image.<\/li>\n\n<li><code>Fit::Crop<\/code> resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image.<\/li><\/ul><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-48c90ced1b32b75586c85fcb5450afb7\" id=\"crop\">Crop<\/h3><p>By calling the <code>crop<\/code> method, part of the image will be cropped to the given <code>$width<\/code> and <code>$height<\/code> dimensions (pixels). Use <code>$cropMethod<\/code> to specify which part will be cropped out.<\/p><p>The following <code>CropPosition<\/code>s are available on the enum: <code>TopLeft<\/code>, <code>Top<\/code>, <code>TopRight<\/code>, <code>Left<\/code>, <code>Center<\/code>, <code>Right<\/code>, <code>BottomLeft<\/code>, <code>Bottom<\/code>, <code>BottomRight<\/code>.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.jpg')\n    -&gt;crop(250, 250, CropPosition::TopRight)\n    -&gt;save();<\/code><\/pre><div class=\"wp-block-uagb-advanced-heading uagb-block-926f06db\"><h2 class=\"uagb-heading-text\">Manipulating the Image<\/h2><\/div><p><\/p><p>Spatie Image provides a number of other methods to manipulate an image.<\/p><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-2c1e7d6dcf9c70d884715566b7d79d4b\" id=\"adjustments\">Adjustments<\/h3><p>You can adjust the brightness, contrast, and gamma of the image.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.jpg')\n    -&gt;brightness(-20)\n    -&gt;save();<\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-97c899828313181dd987c0b1694300df\" id=\"effects\">Effects<\/h3><p>You can add effects like blur, pixelate, grayscale and sepia to the image.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.jpg')\n    -&gt;blur(20)\n    -&gt;save();<\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-95dd4516c1c2893d5c0bf8e442c78a47\" id=\"watermarks\">Watermarks<\/h3><p>You can add another image as a watermark on the image.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.jpg')\n    -&gt;watermark('watermark.png', AlignPosition::center)\n    -&gt;save();<\/code><\/pre><h3 class=\"wp-block-heading has-ast-global-color-2-color has-text-color has-link-color wp-elements-05a5d0b59b81487b1a1b18b1e7bc653f\" id=\"text\">Text<\/h3><p>You can add text to the image.<\/p><pre class=\"wp-block-code\"><code>Image::load('example.jpg')\n    -&gt;text('Hello!')\n    -&gt;save();<\/code><\/pre><p>Of course, you can combine all of these manipulations in a single call. For example, if we want to create a small, grayscale version of the image, we could do something like this:<\/p><pre class=\"wp-block-code\"><code>Image::load('example.jpg')\n    -&gt;resize(64, 64)\n    -&gt;greyscale()\n    -&gt;brighten(20)\n    -&gt;save();<\/code><\/pre><p>You can view the documentation <a href=\"https:\/\/spatie.be\/docs\/image\/v3\/introduction\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for more details about the library.<\/p>","protected":false},"excerpt":{"rendered":"<p>When building a website in Laravel (or any other PHP-based framework) where users will be uploading images, you will probably use a library to manipulate and process those images. With cell phone cameras capturing larger, higher-resolution images, raw images that are uploaded could quickly fill the available hard drive space and cause performance issues on &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.directimpactsolutions.com\/en\/resize-an-image-in-php\/\"> <span class=\"screen-reader-text\">How to Resize an Image in PHP<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":9,"featured_media":17076,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","footnotes":""},"categories":[32],"tags":[87,309,311],"class_list":["post-17075","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-applications","tag-laravel","tag-php","tag-resize-image"],"uagb_featured_image_src":{"full":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php.jpg",1365,768,false],"thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-150x150.jpg",150,150,true],"medium":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-768x432.jpg",768,432,true],"large":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php.jpg",1365,768,false],"2048x2048":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php.jpg",1365,768,false],"woocommerce_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-300x300.jpg",300,300,true],"woocommerce_single":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-600x338.jpg",600,338,true],"woocommerce_gallery_thumbnail":["https:\/\/www.directimpactsolutions.com\/wp-content\/uploads\/2024\/05\/Resize-an-image-in-php-100x100.jpg",100,100,true]},"uagb_author_info":{"display_name":"Alan Bruce","author_link":"https:\/\/www.directimpactsolutions.com\/en\/author\/alan-bruce\/"},"uagb_comment_info":0,"uagb_excerpt":"When building a website in Laravel (or any other PHP-based framework) where users will be uploading images, you will probably use a library to manipulate and process those images. With cell phone cameras capturing larger, higher-resolution images, raw images that are uploaded could quickly fill the available hard drive space and cause performance issues on&hellip;","_links":{"self":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/17075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/comments?post=17075"}],"version-history":[{"count":3,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/17075\/revisions"}],"predecessor-version":[{"id":19962,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/posts\/17075\/revisions\/19962"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media\/17076"}],"wp:attachment":[{"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/media?parent=17075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/categories?post=17075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.directimpactsolutions.com\/en\/wp-json\/wp\/v2\/tags?post=17075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}