I'm having a hard time implemeting the php code will ensure that all uploaded photos will be oriented correctly upon upload.
here is my upload.php function ...
function process_image_upload($field)
{
if(!isset($_FILES[$field]['tmp_name']) || ! $_FILES[$field]['name'])
{
return 'empty';
}
$temp_image_path = $_FILES[$field]['tmp_name'];
$temp_image_name = $_FILES[$field]['name'];
list($iwidth, $iheight, $temp_image_type) =
getimagesize($temp_image_path);
if ($temp_image_type === NULL) {
return false;
}
elseif( $iwidth < 400 )
return 'size';
switch ($temp_image_type) {
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
move_uploaded_file($temp_image_path, $uploaded_image_path);
$thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION .
preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name);
$main_image_path = MAIN_IMAGE_DESTINATION . preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name);
$result =
generate_image_thumbnail($uploaded_image_path,
$thumbnail_image_path,150,150
); if( $result )
$result =
generate_image_thumbnail($uploaded_image_path,$main_image_path,400,400,
true);
return $result ? array($uploaded_image_path, $thumbnail_image_path) :
false;
}
if(isset($_POST['upload'])):
$result = process_image_upload('image');
if ($result === false) {
update_option('meme_message','<div class="error" style="margin-
left:0;"><p>An error occurred while processing upload</p></div>');
} else if( $result === 'empty')
{
update_option('meme_message','<div class="error" style="margin-
left:0;"><p>Please select a Image file</p></div>');
}
elseif( $result === 'size')
{
update_option('meme_message','<div class="error" style="margin-
left:0;"><p>Image width must be greater than 400px;</p></div>');
}
else {
update_option('meme_message','<div style="margin-left:0;"
class="updated"><p>Image uploaded successfully</p></div>');
$guploaderr = false;
$count = intval(get_option('meme_image_count'));
update_option('meme_image_count', $count+1);
}
endif;
and here is some php that I know will work to rotate... but I don't know how to implement.
<?php
$image =
imagecreatefromstring(file_get_contents($_FILES['image_upload']
['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>
Related
I'm looking to add a force download function to the file exploere script below. I need to force the user to download the file when he click on the $namehref, instead of opening the file in the broswer.
How do i add that to the code?
<?php
// Adds pretty filesizes
function pretty_filesize($file) {
$size = filesize($file);
if ($size < 1024) {
$size = $size . " Bytes";
} elseif (($size < 1048576) && ($size > 1023)) {
$size = round($size / 1024, 1) . " KB";
} elseif (($size < 1073741824) && ($size > 1048575)) {
$size = round($size / 1048576, 1) . " MB";
} else {
$size = round($size / 1073741824, 1) . " GB";
}
return $size;
}
date_default_timezone_set("Europe/Rome");
// Checks to see if veiwing hidden files is enabled
/*
if ($_SERVER['QUERY_STRING'] == "hidden") {
$hide = "";
$ahref = "./";
$atext = "Hide";
} else {
$hide = ".";
$ahref = "./?hidden";
$atext = "Show";
}
*/
$hide = ".";
if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || substr($_SERVER['QUERY_STRING'],0,2) == ".." || strstr($_SERVER['QUERY_STRING'], "..")) {
$currdir = ".";
} else {
$currdir = urldecode($_SERVER['QUERY_STRING']);
}
if ($currdir == ".")
$label = "Root";
else {
$path = explode('/', $currdir);
$label = $path[count($path)-1];
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" href="favicon.ico">
<title>DPP</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="sorttable.js"></script>
</head>
<body>
<div id="container" style="margin-top:-50px">
<table class="sortable">
<thead>
<tr>
<th bgcolor="#003399">Filename</th>
<th bgcolor="#003399">Type</th>
</tr>
</thead>
<tbody><?php
error_reporting(0);
// Opens directory
$myDirectory = opendir($currdir);
// Set Forbidden Files
$forbiddenExts = array("php", "ico", "html", "LCK", "js", "css");
// Gets Each Entry
while($entryName = readdir($myDirectory)) {
$exts = explode(".", $entryName);
if(!in_array($exts[1],$forbiddenExts)) {
$dirArray[] = $entryName;
}
}
// Closes directory
closedir($myDirectory);
// Counts elements in array
$indexCount = count($dirArray);
// Sorts files
//sort($dirArray);
// Loops through the array of files
for ($index = 0; $index < $indexCount; $index++) {
// Decides if hidden files should be displayed, based on query above.
if (substr("$dirArray[$index]", 0, 1) != $hide || ($currdir != '.' && $dirArray[$index] == "..")) {
// Resets Variables
$favicon = "";
$class = "file";
// Gets File Names
$name = $dirArray[$index];
$namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index];
$fullname = $currdir . '/' . $dirArray[$index];
// Gets Date Modified
$modtime = date("M j Y g:i A", filemtime($fullname));
$timekey = date("YmdHis", filemtime($fullname));
// Separates directories, and performs operations on those directories
if (is_dir($currdir . '/' . $dirArray[$index])) {
$extn = "<Folder>";
$size = "<Folder>";
$sizekey = "0";
$class = "dir";
// Gets favicon.ico, and displays it, only if it exists.
if (file_exists("$namehref/favicon.ico")) {
$favicon = " style='background-image:url($namehref/favicon.ico);'";
$extn = "<Website>";
}
// Cleans up . and .. directories
if ($name == ".") {
$name = ". (Current Directory)";
$extn = "<System Dir>";
$favicon = " style='background-image:url($namehref/.favicon.ico);'";
}
if ($name == "..") {
$name = ".. (Return to Parent Folder)";
$extn = "<System Dir>";
}
if ($currdir == "." && $dirArray[$index] == "..")
$namehref = "";
elseif ($dirArray[$index] == "..") {
$dirs = explode('/', $currdir);
unset($dirs[count($dirs) - 1]);
$prevdir = implode('/', $dirs);
$namehref = '?' . $prevdir;
}
else
$namehref = '?' . $namehref;
}
// File-only operations
else {
// Gets file extension
$extn = pathinfo($dirArray[$index], PATHINFO_EXTENSION);
// Prettifies file type
switch ($extn) {
case "png": $extn = "PNG Image";
break;
case "jpg": $extn = "JPEG Image";
break;
case "ppsx": $extn = "Microsoft Power Point";
break;
case "jpeg": $extn = "JPEG Image";
break;
case "svg": $extn = "SVG Image";
break;
case "gif": $extn = "GIF Image";
break;
case "ico": $extn = "Windows Icon";
break;
case "txt": $extn = "Text File";
break;
case "log": $extn = "Log File";
break;
case "htm": $extn = "HTML File";
break;
case "html": $extn = "HTML File";
break;
case "xhtml": $extn = "HTML File";
break;
case "shtml": $extn = "HTML File";
break;
case "ppt": $extn = "Microsoft Power Point";
break;
case "js": $extn = "Javascript File";
break;
case "css": $extn = "Stylesheet";
break;
case "pdf": $extn = "PDF Document";
break;
case "xls": $extn = "Spreadsheet";
break;
case "xlsx": $extn = "Spreadsheet";
break;
case "doc": $extn = "Microsoft Word Document";
break;
case "docx": $extn = "Microsoft Word Document";
break;
case "zip": $extn = "ZIP Archive";
break;
case "htaccess": $extn = "Apache Config File";
break;
case "exe": $extn = "Windows Executable";
break;
default: if ($extn != "") {
$extn = strtoupper($extn) . " File";
} else {
$extn = "Sconosciuto";
} break;
}
// Gets and cleans up file size
$size = pretty_filesize($fullname);
$sizekey = filesize($fullname);
}
// Output
echo("
<tr class='$class'>
<td><a href='$namehref'$favicon class='name'>$name</a></td>
<td><a href='$namehref'>$extn</a></td>
</tr>");
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
use this line:
header("Content-Disposition: attachment;Filename={FILE_NAME}");
since it modifies header information, it should be placed above all echos
header('Content-type: application/ms-excel'); // file type
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=\"" .$filename . "\"");
header("Cache-control: private");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: no-cache');
I have this function
public static function upload($path_folder, $input_file_name) {
$photo_file = $_FILES[$input_file_name]; // FILE NAME
$photo_name = $photo_file['name']; // PHOTO NAME
$photo_tmp_name = $photo_file['tmp_name']; // TMP NAME
$photo_extension = pathinfo($photo_name, PATHINFO_EXTENSION); // EXTENSION
// soon GIFs
$rand_num = random_int(1, 3);
switch ($rand_num) {
case 1:
$hash = mb_strtoupper(Hash::create(), 'UTF-8');
break;
case 2:
$hash = mb_strtolower(Hash::create(), 'UTF-8');
break;
case 3:
$hash = str_shuffle(Hash::create());
break;
default:
$hash = Hash::create();
break;
}
$photo_with_extension = $hash . '.' . $photo_extension; // NEW FILE NAME
$factory_destination = 'photos/factory/' . $photo_with_extension; // FACTORY
if(move_uploaded_file($photo_tmp_name, $factory_destination)) {
if(file_exists($factory_destination)) {
if(
($photo_extension === 'jpg') ||
($photo_extension === 'jpeg') ||
($photo_extension === 'JPG') ||
($photo_extension === 'JPEG') ||
($photo_extension === 'png') ||
($photo_extension === 'PNG')
) {
if(
($photo_extension === 'jpg') ||
($photo_extension === 'jpeg') ||
($photo_extension === 'JPG') ||
($photo_extension === 'JPEG')
) {
if(imagecreatefromjpeg($factory_destination)) { // JPEG
$photo_create = imagecreatefromjpeg($factory_destination);
}
}
if(
($photo_extension === 'png') ||
($photo_extension === 'PNG')
) {
if(imagecreatefrompng($factory_destination)) { // PNG
$photo_create = imagecreatefrompng($factory_destination);
}
}
if(getimagesize($photo_tmp_name)) {
list($photo_width, $photo_height) = getimagesize($photo_tmp_name); // WIDTH & HEIGHT
$photo_new_width = 300; // NEW WIDTH
$photo_new_height = 300;
//$photo_new_height = ($photo_height / $photo_width) * $photo_new_width; // NEW HEIGHT
$true_color = imagecreatetruecolor($photo_new_width, $photo_new_height); // TRUE COLOR
if(imagecopyresampled($true_color, $photo_create, 0, 0, 0, 0, $photo_new_width, $photo_new_height, $photo_width, $photo_height)) {
$photo_copy = imagecopyresampled($true_color, $photo_create, 0, 0, 0, 0, $photo_new_width, $photo_new_height, $photo_width, $photo_height); // COPY
$mime = mime_content_type($factory_destination);
if(($mime === 'image/jpeg') || ($mime === 'image/png')) {
switch ($path_folder) {
case 1:
$folder = 'profile';
break;
case 2:
$folder = 'identities';
break;
default:
$folder = 'errors';
break;
}
$new_destination = 'photos/' . $folder . '/';
//$image = imagecreatefromstring(file_get_contents($photo_tmp_name));
$exif = exif_read_data($factory_destination);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$true_color = imagerotate($true_color,90,0);
break;
case 3:
$true_color = imagerotate($true_color,180,0);
break;
case 6:
$true_color = imagerotate($true_color,-90,0);
break;
}
}
if(imagejpeg($true_color, $new_destination . $photo_with_extension, 75)) { // DESIGNATED PATHS
imagedestroy($true_color); // return
if(unlink($factory_destination)) {
return $photo_with_extension; // AUTO UPDATE ADMIN THAT THERE IS A PROBLEM
} else { return 'unlink last'; }
} else { return 'imagejpeg'; }
} else { return 'mime'; }
/*move*/
} else { return 'resampled'; }
} else { return 'get image:' . $photo_tmp_name; }
} else { return 'wrong ext'; }
} else { return 'default2.png'; }
} else { return 'move'; }
return empty($photo_with_extension) ? '' : 'false';
}
the function works upto move_uploaded_file() after that nothing is happening.
This is on production, GD enabled and compatible, Linux server. Where do you think that my codes went wrong ? This works perfectly fine on my localhost though.
To be precise, the imagejpeg() is the one that is not working
New info;
It seems that the problem occurs on getimagesize($photo_tmp_name) it returns false
Newer info:
the tmp_name that given on getimagesize() is correct when I echo it, but it returns false.
if after move_uploaded_file() func,it should be destroy the tmp_file,you could replace the tmp_file with the moved files already,as php will execute the process blocked,so that is safe to do!
i am working on php function that would put watermark on image. I´ve got it working but i need to scale this watermark so the height of the watermark will be 1/3 of the original image. I can do that, but when i put it into my code it just doesnt work, because the parameter of imagecopymerge must be resource, which i dont know what means.
define('WATERMARK_OVERLAY_IMAGE', 'watermark.png');
define('WATERMARK_OVERLAY_OPACITY', 100);
define('WATERMARK_OUTPUT_QUALITY', 100);
function create_watermark($source_file_path, $output_file_path)
{
list($source_width, $source_height, $source_type) = getimagesize($source_file_path);
if ($source_type === NULL) {
return false;
}
switch ($source_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_file_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_file_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_file_path);
break;
default:
return false;
}
$overlay_gd_image = imagecreatefrompng(WATERMARK_OVERLAY_IMAGE);
$overlay_width = imagesx($overlay_gd_image);
$overlay_height = imagesy($overlay_gd_image);
//THIS PART IS WHAT SHOULD RESIZE THE WATERMARK
$source_width = imagesx($source_gd_image);
$source_height = imagesy($source_gd_image);
$percent = $source_height/3/$overlay_height;
// Get new sizes
$new_overlay_width = $overlay_width * $percent;
$new_overlay_height = $overlay_height * $percent;
// Load
$overlay_gd_image_resized = imagecreatetruecolor($new_overlay_width, $new_overlay_height);
// Resize
$overlay_gd_image_complet = imagecopyresized($overlay_gd_image_resized, $overlay_gd_image, 0, 0, 0, 0, $new_overlay_width, $new_overlay_height, $overlay_width, $overlay_height);
//ALIGN BOTTOM, RIGHT
if (isset($_POST['kde']) && $_POST['kde'] == 'pravo') {
imagecopymerge(
$source_gd_image,
$overlay_gd_image_complet,
$source_width - $new_overlay_width,
$source_height - $new_overlay_height,
0,
0,
$new_overlay_width,
$new_overlay_height,
WATERMARK_OVERLAY_OPACITY
);
}
if (isset($_POST['kde']) && $_POST['kde'] == 'levo') {
//ALIGN BOTTOM, LEFT
imagecopymerge(
$source_gd_image,
$overlay_gd_image,
0,
$source_height - $overlay_height,
0,
0,
$overlay_width,
$overlay_height,
WATERMARK_OVERLAY_OPACITY
);
}
imagejpeg($source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY);
imagedestroy($source_gd_image);
imagedestroy($overlay_gd_image);
imagedestroy($overlay_gd_image_resized);
}
/*
* Uploaded file processing function
*/
define('UPLOADED_IMAGE_DESTINATION', 'originals/');
define('PROCESSED_IMAGE_DESTINATION', 'images/');
function process_image_upload($Field)
{
$temp_file_path = $_FILES[$Field]['tmp_name'];
/*$temp_file_name = $_FILES[$Field]['name'];*/
$temp_file_name = md5(uniqid(rand(), true)) . '.jpg';
list(, , $temp_type) = getimagesize($temp_file_path);
if ($temp_type === NULL) {
return false;
}
switch ($temp_type) {
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
$processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace('/\\.[^\\.]+$/', '.jpg', $temp_file_name);
move_uploaded_file($temp_file_path, $uploaded_file_path);
$result = create_watermark($uploaded_file_path, $processed_file_path);
if ($result === false) {
return false;
} else {
return array($uploaded_file_path, $processed_file_path);
}
}
$result = process_image_upload('File1');
if ($result === false) {
echo '<br>An error occurred during file processing.';
} else {
/*echo '<br>Original image saved as ' . $result[0] . '';*/
echo '<br>Odkaz na obrazek je zde';
echo '<br><img src="' . $result[1] . '" width="500px">';
echo '<br><div class="fb-share-button" data-href="' . $result[1] . '" data-type="button"></div>';
}
This code is not posting anything, anywhere. Its not my upload script, it was taken from
http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
It works fine as long as I put an external file in the form action field, with these two functions inside it. But why wont it work as a post to self?
<form action="" method="post" encytype="multipart/form-data">
Upload an image for processing<br>
<input type="file" name="Image1"><br>
<input type="submit" value="Upload">
</form>
<?php
if (isset($_POST['submit'])) {
$result = process_image_upload('Image1');
if ($result === false) {
echo '<br>An error occurred while processing upload';
} else {
echo '<br>Uploaded image saved as ' . $result[0];
echo '<br>Thumbnail image saved as ' . $result[1];
}
}
/*
* PHP function to resize an image maintaining aspect ratio
* http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
*
* Creates a resized (e.g. thumbnail, small, medium, large)
* version of an image file and saves it as another file
*/
define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);
function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
{
list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
switch ($source_image_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
if ($source_gd_image === false) {
return false;
}
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
} elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
$thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
}
$thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
imagedestroy($source_gd_image);
imagedestroy($thumbnail_gd_image);
return true;
}
/*
* Uploaded file processing function
*/
define('UPLOADED_IMAGE_DESTINATION', './images/');
define('THUMBNAIL_IMAGE_DESTINATION', './thumbnails/');
function process_image_upload($field)
{
$temp_image_path = $_FILES[$field]['tmp_name'];
$temp_image_name = $_FILES[$field]['name'];
list(, , $temp_image_type) = getimagesize($temp_image_path);
if ($temp_image_type === NULL) {
return false;
}
switch ($temp_image_type) {
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
move_uploaded_file($temp_image_path, $uploaded_image_path);
$thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name);
$result = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path);
return $result ? array($uploaded_image_path, $thumbnail_image_path) : false;
}
This is not working because there are no $_POST["submit"] defined. This means that the script isn't getting past if (isset($_POST['submit']))
Change this line:
<input type="submit" value="Upload">
to this:
<input type="submit" name="submit" value="Upload">
I have this extension checker:
$upload_name = "file";
$max_file_size_in_bytes = 8388608;
$extension_whitelist = array("jpg", "gif", "png", "jpeg");
/* checking extensions */
$path_info = pathinfo($_FILES[$upload_name]['name']);
$file_extension = $path_info["extension"];
$is_valid_extension = false;
foreach ($extension_whitelist as $extension) {
if (strcasecmp($file_extension, $extension) == 0) {
$is_valid_extension = true;
break;
}
}
if (!$is_valid_extension) {
echo "{";
echo "error: 'ext not allowed!'\n";
echo "}";
exit(0);
}
And then i added this:
if (exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_GIF
OR exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_JPEG
OR exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_PNG) {
echo "{";
echo "error: 'This is no photo..'\n";
echo "}";
exit(0);
}
As soon when I added this to my imageupload function, the function stops working. I dont get any errors, not even the one i made myself "this is no photo", what could be wrong?
Just checked with my host. They support the function exif_imagetype()
Here is the example with the switch usage:
switch(exif_imagetype($_FILES[$upload_name]['name'])) {
case IMAGETYPE_GIF:
case IMAGETYPE_JPEG:
case IMAGETYPE_PNG:
break;
default:
echo "{";
echo "error: 'This is no photo..'\n";
echo "}";
exit(0);
}
Your script can be used too, but you have to put AND between conditions:
if (exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_GIF
AND exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_JPEG
AND exif_imagetype($_FILES[$upload_name]['name']) != IMAGETYPE_PNG) {
echo "{";
echo "error: 'This is no photo..'\n";
echo "}";
exit(0);
}
There's a better, shorter way to get image types, which I'm currently using (can't be sure if it will works as it is not documented in PHP.net as image_type_to_mime_type() can also take an integer as input):
function get_image_type( $image_path_or_resource ) {
$img = getimagesize( $image_path_or_resource );
if ( !empty( $img[2] ) ) // returns an integer code
return image_type_to_mime_type( $img[2] );
return false;
}
echo get_image_type( 'somefile.gif' );
// image/gif
echo get_image_type( 'path/someotherfile.jpg' );
// image/jpeg
$image = get_image_type( 'somefile.gif' );
if ( !empty( $image ) ){
// fine, let's do some more coding
} else {
// bad image :(
}
if (exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_JPEG
AND exif_imagetype($_FILES['image']['tmp_name']) != IMAGETYPE_PNG)
{
enter code here
}
You can use like that. Hope this is will be helpful