Reputation: 438
<?php
if(isset($_POST['upload'])){
// Getting file name
$filename = $_FILES['imagefile']['name'];
// Valid extension
$valid_ext = array('png','jpeg','jpg');
// Location
$location = "images/".$filename;
// file extension
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
// Check extension
if(in_array($file_extension,$valid_ext)){
// Compress Image
compressImage($_FILES['imagefile']['tmp_name'],$location,60);
}else{
echo "Invalid file type.";
}
}
// Compress image
function compressImage($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
}
?>
That work great but I cant figure it out how to rename the image file when is saved. I know how to rename it with move_uploaded_file() but doesn't work with compress_image function.
I tried adding/changing variables but didn't work I get to save the image with the original name on it. Any ideas?
Upvotes: 1
Views: 4208
Reputation: 438
I found a solution to my problem, if someone arrives here with the same question its for you:
index.php
<!doctype html>
<html>
<head>
<title>How to Compress Image and Rename while Uploading with PHP</title>
</head>
<body>
<?php
if(isset($_POST['upload'])){
// Getting file name
$filename = $_FILES['imagefile']['name'];
// Valid extension
$valid_ext = array('png','jpeg','jpg');
// Location
$location = "images/".$filename;
$imageName = "myNewImageName"; // New image name.
// file extension
$file_extension = pathinfo($location, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
$path = "../withdrawals_receipt_uploads/".$imageName.'.'.$file_extension;
// Check extension
if(in_array($file_extension,$valid_ext)){
// Compress Image
compressImage($_FILES['imagefile']['tmp_name'], $path, 60);
}else{
echo "Invalid file type.";
}
}
// Compress image
function compressImage($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
}
?>
<!-- Upload form -->
<form method='post' action='' enctype='multipart/form-data'>
<input type='file' name='imagefile' >
<input type='submit' value='Upload' name='upload'>
</form>
</body>
</html>
Thanks to @Joseph_J
Upvotes: 2
Reputation: 3669
If you use the imagecreatefromstring() function you do not need to check for the MIME type.
In fact imagecreatefromstring()
will return false if it cannot create a resource, meaning the string was not a valid image.
So you can use that as an additional and more reliable test to make sure you have a valid image.
I have went through your code and changed some things and commented along the way.
if(isset($_POST['upload'])){
//Getting file name
$filename = $_FILES['imagefile']['name'];
//Valid extension
$valid_ext = array('png','jpeg','jpg');
//Location
$path = 'images/'; //<--Just the path to the directory.
$imageName = 'myNewImageName.jpg'; //<---Rename your image.
//File extension
$file_extension = strtolower(pathinfo($location, PATHINFO_EXTENSION));
//Check extension
if(in_array($file_extension, $valid_ext)){
//Compress Image
compressImage($filename, $path, $imageName, 60);
}else{
echo "Invalid file type.";
}
}
//Compress image
function compressImage($file, $path, $imageName, $quality) {
//Check to see if the directory exist. If not create it with write permissions.
if(!file_exists($path)){
mkdir($path, 0777);
}
//Check to see if the directory is writable. If not change permissions.
if(!is_writable($path)){
chmod($path, 0777);
}
$resource = imagecreatefromstring(file_get_contents($file)); //Creates an image resource and it does not matter what MIME type.
imagejpeg($resource, $path . $imageName, $quality); //Will save image to new path and filename.
imagedestroy($resource); //<--Don't forget to free your resources.
//Change your permissions back to what they need to be.
chmod($path, 0755);
chmod($destination, 0644);
}
Upvotes: 2