PHP - Web Design - How to resize images during uploading - Full tutorial

Поділитися
Вставка
  • Опубліковано 18 вер 2024
  • how to resize images during the uploading process to save disk space and make sure your website loads quickly
    Support me on patreon: / quickcode

КОМЕНТАРІ • 107

  • @drewpeer
    @drewpeer 4 роки тому +5

    Most the time the images will be huge in scale when a user uploads something. Now they don't stand a chance! Thanks for explaining it!

  • @BarnabyPerrinAldous
    @BarnabyPerrinAldous 4 роки тому +1

    Thanks, great tutorial which helped me through a repeat sticking patch. To those saying it doesn't work - check your directory permissions, particularly if you get errors such as:
    Warning: move_uploaded_file(XXXXXXXXXXX): failed to open stream: Permission denied ...

  • @zuhaibkhan7540
    @zuhaibkhan7540 2 роки тому +2

    It worked perfectly.

  • @akanbitobextinz7471
    @akanbitobextinz7471 3 роки тому +2

    Absolute, this helped me.

    • @QuickProgramming
      @QuickProgramming  3 роки тому +1

      Awesome☺️

    • @akanbitobextinz7471
      @akanbitobextinz7471 3 роки тому +1

      @@QuickProgramming Thanks. Can we change the background color of the image while uploading it. Like to white or a dark color just like what photo editor app will do??

    • @akanbitobextinz7471
      @akanbitobextinz7471 3 роки тому

      @@QuickProgramming something like, removing the initial background of the image, taking out only the object from the image and adding a white background color to it. Like what remove.bg will do 😀😀

  • @nassambetty
    @nassambetty 4 місяці тому +1

    Nice one!
    Thanks man! 👍

  • @orph4nsx
    @orph4nsx 4 роки тому +1

    Thanks

  • @troupebase2292
    @troupebase2292 3 роки тому +3

    Great tutorial! Will this also reduce the file size?

  • @christianfoxy45
    @christianfoxy45 2 роки тому +1

    Wow this worked great, thank you so much for making something quit difficult understandable!!!

    • @QuickProgramming
      @QuickProgramming  2 роки тому

      thanks for the awesome feedback, and you're most welcome :))

  • @Verblator
    @Verblator 6 років тому +2

    Thanks for the effort. It's a great toturial . Plese add the " loop function idea" to how to resize multiple images or at least refer us to a nice video like yours. I saw other people asking the same. Plz reply and Thanks again.

    • @QuickProgramming
      @QuickProgramming  6 років тому +1

      Thanks for the comment, i'll be uploading that video very soon

    • @QuickProgramming
      @QuickProgramming  6 років тому +6

      hey i managed to make the video you requested on uploading multiple images. here's the link ua-cam.com/video/-AsRT9iwMJ8/v-deo.html

  • @mesfunghezehiei4679
    @mesfunghezehiei4679 4 роки тому +2

    it work fine but when i upload image from phone the image rotate when resize how to solve it

    • @QuickProgramming
      @QuickProgramming  4 роки тому

      its easy to solve. every image has meta data which includes its orientation. this data is lost once you resize the image. so just rotate the image then, resize it. i have a tutorial on how to resize here ua-cam.com/video/OwDA6i3tFAo/v-deo.html

  • @TheDr660
    @TheDr660 2 роки тому +1

    Has anyone had the issue that this does not work on long photos? The only problem I've run across is some random re-angling which OP has given answer how to fix below, but I have one long photo that is 257kb that it just does nothing to and loads into the folder. Every other photo works perfectly and re-crops very well. Any one else run into this?

    • @QuickProgramming
      @QuickProgramming  2 роки тому +1

      by 'long photo', do you mean a photo with bigger height than width?

    • @TheDr660
      @TheDr660 2 роки тому +1

      @@QuickProgramming Hi! Yes its dimensions are 1200 × 1800.

  • @Adrianjst
    @Adrianjst 2 роки тому +1

    Hi.
    Why i cant insert the image in a database after have been resized ??

  • @overidon
    @overidon 2 роки тому +1

    great video thanks

  • @osama6795
    @osama6795 4 роки тому +2

    awesome tutorial, but can u make a tutorial on how to do this resizing for all image types?

    • @QuickProgramming
      @QuickProgramming  4 роки тому +2

      yes i did. you can watch this one which shows how to upload multiple images at once. there are only 2 parts to change in the code to shift from jpeg to png. change imagecreatefromjpeg to imagecreatefrompng and also change imagejpeg to imagepng. you can also make it automatic. the tutorial is here in 3 parts:
      part 1:
      ua-cam.com/video/-AsRT9iwMJ8/v-deo.html
      part 2:
      ua-cam.com/video/fyTejYCNprM/v-deo.html
      part 3:
      ua-cam.com/video/V6bnPxaTcF0/v-deo.html

  • @Faks_Ar
    @Faks_Ar 6 років тому +1

    Well, my head exploded.. but it works fine and I kinda understand it. Will watch after a good sleep. Thanks for the guide, hope you dont mind on me to be your 100 subscriber!

    • @QuickProgramming
      @QuickProgramming  6 років тому +1

      thanks for the sub :)

    • @Faks_Ar
      @Faks_Ar 6 років тому

      No problem! I have a question tho. How could I config it to resize to the max_resolution using the smallest side? That way the resulting image has that new size as the smallest side.

    • @QuickProgramming
      @QuickProgramming  6 років тому +1

      ok, if i get you correctly, what you need to do is first find out the resolution of the image, then check which is smaller, then set that small value as max_resolution. to find out the a jpg resolution, you can use a function called exif like this:
      $data = exif_read_data($image_path);
      the variable $data will then contain very useful info, feel free to check using the print_r function. The res of the image will be in:
      $height = $data['COMPUTED']['Height'];
      $width = $data['COMPUTED']['Width'];
      take note of the capitalization. then use an if statement to check which is bigger e.g
      if($height < $width){
      $max_resolution = $height;
      }else{
      $max_resolution = $width;
      }
      if this isn't what you were asking for, feel free to elaborate some more

    • @Faks_Ar
      @Faks_Ar 6 років тому

      Totally forgot about this, srry! That should do, intresting variables :D Thanks!

  • @selvin3422
    @selvin3422 6 років тому +3

    Wonderful video. Thank you so much. This works perfectly for me. Do you have a video on resizing multiple images during upload?

    • @QuickProgramming
      @QuickProgramming  6 років тому

      thank you :) i dont have that video yet but ill make one soon since you've asked. its not that different from uploading one image. only that you create a loop to go through multiple files and run the same functions on them one by one.

    • @selvin3422
      @selvin3422 6 років тому

      Thank you and I'll be looking forward to that. Two questions, 1) how can I resize the image in png, gif, jpg or all 3 together as once? Currently it only resizes in jpeg and will not accept png or gif file.
      Q#2
      I created these variables for the image name and temp location.
      $user_image = $_FILES['image']['name'];
      $user_image_temp = $_FILES['image']['tmp_name'];
      Now, when I call these variables in the move_uploaded_file, the file is uploaded, but it won't resize. What I am doing wrong? I highly appreciate your help.
      move_uploaded_file($user_image_temp, $user_image);

    • @QuickProgramming
      @QuickProgramming  6 років тому +1

      to resize other file types, you can simply replace the function imagecreatefromjpeg with imagecreatefrompng and imagecreatefromgif. the rest remains the same. you can first check for the file extension to know what type of file it is using this: $extension = \strtolower(\pathinfo($file, \PATHINFO_EXTENSION)); or you can check the $_FILES['image'['type'] since extensions can be fake sometimes. whether you choose to save them back to their original format is up to you, because its possible to save to any format no matter what format the original is, using imagejpeg() or imagepng() or imagegif().
      about your error, the function move_uploaded_file() only uploads the file from the local computer to the server. so it does no resizing in the process. you then have to run the resize function on the uploaded file after running move_uploaded_file(). i hope that helps

    • @selvin3422
      @selvin3422 6 років тому

      Yes, this helps. My image uploads, but won't crop and also png and gif won't work. I have pasted my code below can you please help why it won't work?. Thank you so much for you time!

    • @QuickProgramming
      @QuickProgramming  6 років тому +2

      ok, it seems you're not assigning the file name properly. try this. change this line:
      $user_image = $_FILES['image']['name'];
      to..
      $user_image = "../images/" . $_FILES['image']['name'];
      then change this line:
      // Move image to a temporary local location
      move_uploaded_file($user_image_temp, "../images/");
      to...
      move_uploaded_file($user_image_temp, $user_image);
      then delete this line entirely:
      // Assign image location to a variable
      // $user_image = "images/". $_FILES['image']['name'];
      in your current setup, move_uploaded_file() is sending your image to an empty location. you're supposed to specify the full path including the filename during the move operation, then use the same file path for the resize.
      let me know what happens when you try it.

  • @SuperJesusceballos
    @SuperJesusceballos 4 роки тому +2

    Works, however on upload echo photo and it is moved 90 degrees positive(right) and then the reduce works and I send it to a file and print to screen from an HTML code link to the root directory of the project folder.HELP

    • @QuickProgramming
      @QuickProgramming  4 роки тому

      i did not add that part to make the tutorial easy, but i can give you the code here

    • @QuickProgramming
      @QuickProgramming  4 роки тому +1

      after you create an image resource with this code:
      $image_resource = imagecreatefromjpeg($image_path);
      add some code to check if image is rotated using this code:
      this code comes just before "imagejpeg" or "imagepng". just before you save the image to file.

    • @QuickProgramming
      @QuickProgramming  4 роки тому +1

      the short version is like this:

  • @elieli3063
    @elieli3063 Рік тому

    Muy bueno el código, primero me dió error y era que no tenía habilitada la librería GD, pero después genial, ahora tengo que adaptarlo para guardar en la base de datos.

  • @ChannelSILHOUETTEnepal
    @ChannelSILHOUETTEnepal 7 місяців тому

    Hi, Could you please help me how to integrate this code with crocoblock's jetformbuilder plugin on wordpress? Metafiled Id for image upload field being "profilepicture". Would be a great help.

  • @skyro6138
    @skyro6138 3 роки тому +1

    This was so helpful! Thank you :)

    • @QuickProgramming
      @QuickProgramming  3 роки тому

      Thanks for the feedback, am glad it was helpful😊

  • @s3d120
    @s3d120 5 років тому +1

    amazing biggest problem solved in short coding

  • @gurjeetsinghkalsi2826
    @gurjeetsinghkalsi2826 4 роки тому +1

    Thanku sir , if uh have any tutorial classes in Udemy please share link , because i love ur teaching style .. love from india

    • @QuickProgramming
      @QuickProgramming  4 роки тому

      thank you Gurjeet. i appreciate the complement. am working on my fort Udemy course. i will you comment back here when its out, thanks. if you have specific subjects you want to learn, let me know so i can include a course on that too :)

    • @chukwumandubuisi856
      @chukwumandubuisi856 3 роки тому

      @@QuickProgramming give us Angular and Ionic 5

  • @dota2bryanbeltran986
    @dota2bryanbeltran986 2 роки тому

    It is for jpeg image? How about other file type? Like png,gif etc

  • @sepehrbastany9103
    @sepehrbastany9103 4 роки тому +1

    thanks so much

  • @69guigz
    @69guigz 3 роки тому +1

    Thanks a lot!

  • @Big_Dot_Inc
    @Big_Dot_Inc Рік тому +1

    GOD like!

  • @RazerSCYNescafe
    @RazerSCYNescafe 3 роки тому +1

    Cool thanks sir

  • @vanqrz
    @vanqrz 3 роки тому +1

    Thanks for sharing. Very useful. Done sub

  • @nawshinahmed2934
    @nawshinahmed2934 4 роки тому

    Hi. Can not upload the file on the server after resizing.

  • @ashiqdey
    @ashiqdey 5 років тому

    awesome

  • @alantravers4264
    @alantravers4264 4 роки тому

    I Just signed up on your Patreon account to get the code but there is nothing there.

  • @sumeetsaini4662
    @sumeetsaini4662 6 років тому +1

    Can we do same with video files?

    • @QuickProgramming
      @QuickProgramming  6 років тому

      yes you can upload them in the same way, but obviously you have to skip the resizing part because it wont work on videos. also change the accepted file type from "image/jpeg" to "video/mp4". everything else should work just fine

  • @mohammedrameez4906
    @mohammedrameez4906 2 місяці тому

    How to save it database?

  • @belowsurfacemedia
    @belowsurfacemedia 3 роки тому

    would love to watch this but the music keeps me from concentrating :(

  • @collins6869
    @collins6869 Місяць тому

    It didn't work for me is there a way you can share the source code

    • @QuickProgramming
      @QuickProgramming  Місяць тому

      @@collins6869 unfortunately I never saved the code for this. What errors do you get?

  • @arkaghoshchowdhury1409
    @arkaghoshchowdhury1409 5 років тому

    If image size is more than 2 mb then resize is not working, how can I solve this ?

    • @QuickProgramming
      @QuickProgramming  5 років тому +1

      its probably the memory limit allocated to PHP that is low. you can change this value by opening the php.ini file, found in your xampp, wampp or mampp folder. just search for it there and edit this line: memory_limit=2MB. yours may be different value but just increase it to something like 16MB or 32MB.
      if you cant find the file, try adding this line at the top of your PHP code:
      ini_set('memory_limit','32M');
      hope it helps :)

    • @arkaghoshchowdhury1409
      @arkaghoshchowdhury1409 5 років тому +1

      Thanks :)

  • @SuperJesusceballos
    @SuperJesusceballos 4 роки тому

    I am successful reducing and rotating the image. Now, I tried to put the image in mySql database. None of the variables I create transfer into my database. I retrieve it and it is blank. I even create a variable that outputs to screen on a link, eg. $im = (

    • @QuickProgramming
      @QuickProgramming  4 роки тому

      never put images inside a database. save the image to a folder and put only the path to the image inside the database.

    • @QuickProgramming
      @QuickProgramming  4 роки тому

      iam making a tutorial on how to create a social website from start to finish. i will show how to do this in detail. upload images, crop resize and save. here is the playlist ua-cam.com/play/PLY3j36HMSHNWaKUC73RJlwi6oU-WTpTPM.html

  • @DrenzAwe
    @DrenzAwe 3 роки тому

    How about resizing before turning into blob and insert to database ?

    • @QuickProgramming
      @QuickProgramming  3 роки тому

      never a good idea to save images directly into the database. there's no goof reason to do that. the file system works better by far

    • @DrenzAwe
      @DrenzAwe 3 роки тому

      @@QuickProgramming yes I know, but the simulation im creating need the images in the database, by path is not working

    • @QuickProgramming
      @QuickProgramming  3 роки тому

      @@DrenzAwe why is it not working using a path? even if you save in the database, its the same as saving to a path. but iof you really need to save to the DB, use base64. it will be easier to display the images. make the column TEXT or VARCHAR. use this function to convert the image:
      base64_encode();
      it will convert the image to string. then use
      base64_decode() to display;

    • @DrenzAwe
      @DrenzAwe 3 роки тому

      @@QuickProgramming im using unity c# language

    • @DrenzAwe
      @DrenzAwe 3 роки тому

      @@QuickProgramming sadly you cannot just use path to make sprite image base64 is needed

  • @poojachouhan3305
    @poojachouhan3305 6 років тому

    plz tell me how to use crop box like facebook and whaysapp provide us

    • @QuickProgramming
      @QuickProgramming  6 років тому

      to do that kind of thing, you require javascript in conjunction with PHP. PHP only works on your server so by the time a users sees a page loading, PHP is already done and cant affect the page. so Javascript would work to create the required crop box, calculate what part needs to be cropped and then send that information to the server using AJAX, so that PHP can then crop the image. ill make a tutorial about that

  • @hamzalaxmidhar7158
    @hamzalaxmidhar7158 7 місяців тому

    Can anyone provide the source code

  • @logancajaras3981
    @logancajaras3981 2 роки тому

    This reduce quality by large margin

    • @QuickProgramming
      @QuickProgramming  2 роки тому

      Increase the resolution or increase the saving quality to 100. e.g
      imagejpeg($resource,$filename,100);

  • @harisjamal4688
    @harisjamal4688 5 років тому

    Can u send me the source code plz?

  • @ashiqdey
    @ashiqdey 5 років тому

    post the code

  • @Dexter101x
    @Dexter101x 6 років тому

    This isn't really resizing per se, it's cropping image, which in fairness I wanted

    • @QuickProgramming
      @QuickProgramming  6 років тому +1

      kind of true actually.... resizing and cropping both use the same function, which is imagecopy. the only difference is the parameters you use. forexample if you copy the image to a smaller image, thats cropping because some of the image pixels will be lost. but if you make sure to paste all pixels, then its resizing. am glad you found it useful :)

    • @QuickProgramming
      @QuickProgramming  6 років тому

      i have another video dedicated to cropping just in case you havent seen it ua-cam.com/users/edit?o=U&video_id=goRD8GwzwAE

    • @Dexter101x
      @Dexter101x 6 років тому

      That link isn't working, you should link a playlist, but I would prefer the actual video

    • @QuickProgramming
      @QuickProgramming  6 років тому

      sorry i got the link from my dashboard, here's the public link ua-cam.com/video/goRD8GwzwAE/v-deo.html

  • @dota2bryanbeltran986
    @dota2bryanbeltran986 2 роки тому

    Didn't work