{"id":16890,"date":"2020-08-01T13:04:42","date_gmt":"2020-08-01T13:04:42","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=16890"},"modified":"2021-10-30T05:45:16","modified_gmt":"2021-10-30T05:45:16","slug":"how-to-upload-and-compress-an-image-using-php","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-upload-and-compress-an-image-using-php\/","title":{"rendered":"How to Upload and Compress an Image using PHP"},"content":{"rendered":"\n<p>Image compression is very helpful to reduce the size of the image. Generally, the user does not optimize the image when uploading through the website. In this case, compress images before upload to optimize the image.<\/p>\n\n\n\n<p>So, now I am show you how to compress image before upload using <strong>PHP.<\/strong><\/p>\n\n\n\n<p>Create a PHP form with an input field and submit button. This <strong>&lt;form&gt;<\/strong><br>tag contain following contain:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>method=&#8221;post&#8221;<\/li><li>enctype=&#8221;multipart\/form-data<\/li><\/ul>\n\n\n\n<p><strong>Note:<\/strong> enctype=&#8221;multipart\/form-data&#8221; is compulsory for image upload precess via post method in form.<\/p>\n\n\n\n<p><strong>index.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/narayanlog\/314201e95516f0eda0f5c89ec500b5a6.js\"><\/script>\n\n\n\n<p>After the form submission, the file data is submitted to the <strong>upload.php<\/strong> file for further processing. Then, compress and upload images in PHP.<\/p>\n\n\n\n<p>The upload.php file handles the image compression upload operations. I have used some <strong>PHP <\/strong>image functions like <strong>compressImage()<\/strong> that helps to compress and save image on the server using <strong>PHP<\/strong>.<\/p>\n\n\n\n<p>If the file is submitted, then retrieve the file info using the <strong>PHP $_FILES<\/strong> method, compressed size, and upload image using <strong>compressImage()<\/strong> function. Then see the image upload status.<\/p>\n\n\n\n<p><strong>upload.php<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/narayanlog\/4e7874782e4f0472e25ce433aacee1ba.js\"><\/script>\n\n\n\n<p>Then, see the results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"151\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image-2-1024x151.png\" alt=\"\" class=\"wp-image-16902\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image-2-1024x151.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image-2-300x44.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image-2-768x113.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image-2.png 1163w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"389\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image2-1024x389.png\" alt=\"\" class=\"wp-image-16898\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image2-1024x389.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image2-300x114.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image2-768x292.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/08\/compress-image2.png 1152w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_14712\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_87541\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_14712\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Image compression is very helpful to reduce the size of the image. Generally, the user does not optimize the image when uploading through the website. In this case, compress images before upload to optimize the image. So, now I am show you how to compress image before upload using PHP. Create a PHP form with&#8230;<\/p>\n","protected":false},"author":9,"featured_media":16899,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[6313,6223,177],"class_list":["post-16890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-compressing","tag-functions","tag-php"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=16890"}],"version-history":[{"count":5,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16890\/revisions"}],"predecessor-version":[{"id":24637,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16890\/revisions\/24637"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/16899"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=16890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=16890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=16890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}