0

How can I resize the image before uploading it in php? On this code that I completed, I want to modify it for the purpose of placing the appropriate size for me before uploading

  • Note that the code works normally and without any problems. I want to change how to scale the image only on the same code variables
if ($_POST['add']){
$img1 = $_FILES["img1"]["name"];
$temp1 = $_FILES["img1"]["tmp_name"];
$rename = "image/".uniqid();
$folder1 = "$rename".$img1;
 $videoFileType = strtolower(pathinfo($folder1,PATHINFO_EXTENSION));
 $extensions_arr = array("jpg","png","jpeg");
if(!in_array($videoFileType,$extensions_arr) {
echo "error";
}else {
$sql = "INSERT INTO -----------"
}
}
schalkneethling
  • 5,832
  • 3
  • 20
  • 18
  • I am grateful for your reply, but it is not as I want to want it in php and with the same variables that I wrote because I am really new to learning and I am lost between the variables and I only need modification – Mustafa Al-Tai May 29 '22 at 19:24
  • PHP runs on the server, you cannot resize the image in PHP before it is uploaded to the server. Perhaps you intend to do something else? – KIKO Software May 29 '22 at 19:31
  • I want to change the sizes of the images that are uploaded to my database to 500 widths and 500 heights, do you understand my idea? – Mustafa Al-Tai May 29 '22 at 19:44
  • Yes, I do, the image has been uploaded, is being processed in PHP and you want to store a smaller version in your database. The term "uploading" mislead me. When talking about databases we normally say: "store" and "retrieve". The image that is uploaded is basically a file, on disk, you need to load that file into PHP, resize it, and then save it as a file again. See [some example answers](https://stackoverflow.com/questions/18805497/php-resize-image-on-upload). – KIKO Software May 29 '22 at 19:54
  • It is strongly discouraged to store images in a database. Images are files, files should be stored in the file system, that is, on disk. You can store information about the image in a database, for instance its path in the file system, and other data. – KIKO Software May 29 '22 at 19:55
  • It is true that I did this. I stored the image in a second server and stored the path and extension of the image in the database – Mustafa Al-Tai May 29 '22 at 19:58
  • Thank you for your effort. As for the examples, I read a lot and being a beginner, I did not succeed in doing what I wanted, so for my code I want to modify it or a simpler way I can succeed in scaling the image – Mustafa Al-Tai May 29 '22 at 20:09
  • Yes, that's a good idea. Start simple. Not with an uploaded image, but with an image that is already on the server. There are lots of examples and tutorials about that. – KIKO Software May 29 '22 at 20:18
  • Thank you very much, I will keep trying to find a solution for sure like you – Mustafa Al-Tai May 29 '22 at 20:35

0 Answers0