Download jpg but not jpg

I have a code block that loads an image and creates a sketch if the extension is a lowercase jpg, but it will not load the image if it is an uppercase JPG. When I rename an image, for example. example.JPG to example.jpg, it will be downloaded. But example.JPG will not load, and I will not get an error. That makes no sense to me. Does anyone have an explanation for this? Here is my code:

<?php function createThumbnail($filename) { require 'config.php'; if(preg_match('/[.](jpg)$/', $filename)) { $im = imagecreatefromjpeg($filename.$path_to_image_directory); } else if (preg_match('/[.](gif)$/', $filename)) { $im = imagecreatefromgif($path_to_image_directory . $filename); } else if (preg_match('/[.](png)$/', $filename)) { $im = imagecreatefrompng($path_to_image_directory . $filename); } $ox = imagesx($im); $oy = imagesy($im); $nx = $final_width_of_image; $ny = $final_height_of_image; $nm = imagecreatetruecolor($nx, $ny); imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy); if(!file_exists($path_to_thumbs_directory)) { if(!mkdir($path_to_thumbs_directory)) { die("There was a problem. Please try again!"); } } imagejpeg($nm, $path_to_thumbs_directory . $filename); $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />'; $tn .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.'; echo $tn; } ?> 

Configuration file:

 <?php $final_width_of_image = 300; $final_height_of_image = 300; $path_to_image_directory = ''; $path_to_thumbs_directory = '-tn'; ?> 
+5
source share
2 answers

In this code block, you only allow lowercase extensions:

 if(preg_match('/[.](jpg)$/', $filename)) { //--lowercase-only--^ $im = imagecreatefromjpeg($filename.$path_to_image_directory); }else if (preg_match('/[.](gif)$/', $filename)) { $im = imagecreatefromgif($path_to_image_directory . $filename); } else if (preg_match('/[.](png)$/', $filename)) { $im = imagecreatefrompng($path_to_image_directory . $filename); } 

You can fix it by placing the OR condition in the regular expression:

 if(preg_match('/[.](jpg|JPG)$/', $filename)) { // -----uppercase-------^ $im = imagecreatefromjpeg($filename.$path_to_image_directory); }else if (preg_match('/[.](gif|GIF)$/', $filename)) { $im = imagecreatefromgif($path_to_image_directory . $filename); } else if (preg_match('/[.](png|PNG)$/', $filename)) { $im = imagecreatefrompng($path_to_image_directory . $filename); } 

Or you can add case sensitivity with the i modifier:

 if(preg_match('/[.](jpg)$/i', $filename)) { // -------modifier--------^ $im = imagecreatefromjpeg($filename.$path_to_image_directory); }else if (preg_match('/[.](gif)$/i', $filename)) { $im = imagecreatefromgif($path_to_image_directory . $filename); } else if (preg_match('/[.](png)$/i', $filename)) { $im = imagecreatefrompng($path_to_image_directory . $filename); } 
+5
source

the best way to do this is to check the file itself, and not the extension, which can always be incorrect.

  $data = getimagesize($filename); switch($data['mime]){ case('image/png): $im = imagecreatefrompng($filename); break; case('img/jpg'): case('image/jpeg'): case('image/pjpeg'): case('image/x-jps'): $im = imagecreatefromjpeg($filename); break; case(self::GIF): $im = imagecreatefromgif($filename); break; } 
0
source

Source: https://habr.com/ru/post/1243176/


All Articles