{"id":8444,"date":"2024-05-03T12:38:39","date_gmt":"2024-05-03T12:38:39","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=8444"},"modified":"2024-05-03T12:38:42","modified_gmt":"2024-05-03T12:38:42","slug":"easy-laravel-image-upload-resize-for-digital-ocean","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/easy-laravel-image-upload-resize-for-digital-ocean\/","title":{"rendered":"Easy Laravel Image Upload &amp; Resize for Digital Ocean"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 2,337<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p><strong>Streamlining Image Upload and Resizing with Laravel and Digital Ocean Spaces<\/strong><\/p>\n\n\n\n<p>In modern web development, efficiently handling image uploads and resizing is paramount for creating engaging user experiences. Laravel, a popular PHP framework, offers robust features for accomplishing these tasks seamlessly. Coupled with Digital Ocean Spaces, a scalable object storage service, developers can elevate their image management capabilities to new heights. Let&#8217;s dive into how to achieve this with code examples.<\/p>\n\n\n\n<p><strong>Setting Up Laravel and Digital Ocean Spaces Integration<\/strong><\/p>\n\n\n\n<p>First, ensure you have Laravel installed and set up in your development environment. Then, integrate Digital Ocean Spaces by installing the <code>league\/flysystem-aws-s3-v3<\/code> package:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-d7d6b68863ab0a3935d69a310e9891dd\"><code><code>composer require league\/flysystem-aws-s3-v3<\/code><\/code><\/pre>\n\n\n\n<p>Next, configure your Digital Ocean Spaces credentials in the <code>config\/filesystems.php<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-bc054a5c69ea76e00d803ffe8e81584d\"><code><code>'do_spaces' => &#91;\n    'driver' => 's3',\n    'key' => env('DO_SPACES_KEY'),\n    'secret' => env('DO_SPACES_SECRET'),\n    'region' => env('DO_SPACES_REGION'),\n    'bucket' => env('DO_SPACES_BUCKET'),\n    'url' => env('DO_SPACES_URL'),\n],<\/code><\/code><\/pre>\n\n\n\n<p><strong>Uploading Images with Laravel<\/strong><\/p>\n\n\n\n<p>Now, let&#8217;s create a controller method for handling image uploads:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-430b935391ba16bdfbbbcde37857aa26\"><code><code>use Illuminate\\Http\\Request;\nuse Illuminate\\Support\\Facades\\Storage;\n\nclass ImageController extends Controller\n{\n    public function upload(Request $request)\n    {\n        $image = $request->file('image');\n        $path = $image->store('images', 'do_spaces');\n\n        return &#91;'url' => Storage::disk('do_spaces')->url($path)];\n    }\n}<\/code><\/code><\/pre>\n\n\n\n<p>This code snippet accepts an image file via a POST request and stores it in Digital Ocean Spaces under the <code>images<\/code> directory. It then returns the URL of the uploaded image.<\/p>\n\n\n\n<p><strong>Resizing Images with Laravel<\/strong><\/p>\n\n\n\n<p>To resize images before uploading them, use the <code>Intervention Image<\/code> package:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-91b25649dc07526989e6eb91f75662a2\"><code><code>composer require intervention\/image<\/code><\/code><\/pre>\n\n\n\n<p>Here&#8217;s how you can resize images before uploading:<\/p>\n\n\n\n<pre class=\"wp-block-code has-vivid-red-color has-text-color has-link-color wp-elements-be6b53d4696e0685c82ce4ad7683a523\"><code><code>use Intervention\\Image\\Facades\\Image;\n\nclass ImageController extends Controller\n{\n    public function uploadAndResize(Request $request)\n    {\n        $image = $request->file('image');\n        $resizedImage = Image::make($image)->resize(300, 200)->encode();\n        $path = Storage::disk('do_spaces')->put('images', $resizedImage);\n\n        return &#91;'url' => Storage::disk('do_spaces')->url($path)];\n    }\n}<\/code><\/code><\/pre>\n\n\n\n<p>This code snippet resizes the image to 300&#215;200 pixels before uploading it to Digital Ocean Spaces.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>By integrating Laravel with Digital Ocean Spaces and utilizing libraries like Intervention Image, developers can effortlessly manage image uploads and resizing in their web applications. Whether you&#8217;re building a portfolio website or a complex e-commerce platform, mastering these techniques will enhance the performance and user experience of your application. With Laravel&#8217;s elegance and Digital Ocean Spaces&#8217; scalability, the possibilities are endless for creating visually stunning and efficient web applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2,337 Views Streamlining Image Upload and Resizing with Laravel and Digital Ocean Spaces In modern web development, efficiently handling image uploads and resizing is paramount for creating engaging user experiences. Laravel, a popular PHP framework, offers robust features for accomplishing these tasks seamlessly. Coupled with Digital Ocean Spaces, a scalable object storage service, developers can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[69],"tags":[],"class_list":["post-8444","post","type-post","status-publish","format-standard","hentry","category-digital-ocean"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=8444"}],"version-history":[{"count":1,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8444\/revisions"}],"predecessor-version":[{"id":8445,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/8444\/revisions\/8445"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=8444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=8444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=8444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}