/******* This code is for converting height with of jpeg, jpg, png and gif image*********
<?php
<?php
function makeName($text) {
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = trim($text, '-');
$text = strtolower($text);
$text = preg_replace('~[^-\w]+~', '', $text);
if (strlen($text) > 70) {
$text = substr($text, 0, 70);
}
if (empty($text)) {
return time();
}
return $text;
}
if (isset($_FILES['files']) && !empty($_FILES['files'])) {?>
$errors = array();
if (isset($_FILES['files']['tmp_name'][0]) && $_FILES['files']['tmp_name'][0]) {
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$file = pathinfo($file_name);
$extension = strtolower($file['extension']);
$new_name = makeName($file['filename'] . date('d-m-Y h:i:s')) . "." . $extension;
//****This code is for converting height with of Jpeg, Jpg, Png and Gif image
$desired_dir = "../../../data/images/";
if (is_dir($desired_dir) == false) {
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
$filepath = $desired_dir . $new_name;
$uploadedfile = $_FILES['files']['tmp_name'][$key];
list($width, $height) = getimagesize($uploadedfile);
if (($width > 1024) || $height > 768 || ($file_size > 300000)) {
if ($width > 1024) {
$newwidth = 1024;
$newheight = ($height / $width) * $newwidth;
} else if ($height > 768) {
$newheight = 768;
$newwidth = ($width / $height) * $newheight;
} else {
$newwidth = $width;
$newheight = $height;
}
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG" || $extension == "JPEG") {
$src = imagecreatefromjpeg($uploadedfile);
} else if ($extension == "png" || $extension == "PNG") {
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
}
$tmp = imagecreatetruecolor($newwidth, $newheight);
// imagecolortransparent($tmp, imagecolorallocatealpha($tmp, 0, 0, 0, 127));
// imagealphablending($tmp, false);
// imagesavealpha($tmp, true);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
if ($file_size > 300000) {
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG" || $extension == "JPEG") {
imagejpeg($tmp, $filepath, 70);
} else if ($extension == "png" || $extension == "PNG") {
imagepng($tmp, $filepath, 7);
} else {
imagegif($tmp, $filepath);
}
} else {
if ($extension == "jpg" || $extension == "jpeg" || $extension == "JPG" || $extension == "JPEG") {
imagejpeg($tmp, $filepath, 90);
} else if ($extension == "png" || $extension == "PNG") {
imagepng($tmp, $filepath);
} else {
imagegif($tmp, $filepath);
}
}
imagedestroy($src);
imagedestroy($tmp);
} else {
if (is_dir($filepath) == false) {
move_uploaded_file($file_tmp, $filepath);
} else { // rename the file if another one exist
$new_dir = $filepath;
rename($file_tmp, $new_dir);
}
}
$size = filesize($filepath);
}
} else {
echo "Please, Upload atlist one file";
}
}