Created
January 16, 2018 03:49
-
-
Save ManishLSN/a2c83fb99ad263910a271ff655a248c7 to your computer and use it in GitHub Desktop.
make thumb of image from the php functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<center> | |
<form action="ab1.php" method="post" enctype="multipart/form-data"> | |
Select image to upload: | |
<input type="file" name="file"><br><br> | |
Desired width: <input type="text" name="desired_width"><br><br> | |
<input type="submit" value="Upload Image" name="submit"> | |
</form> | |
</center> | |
</body> | |
</html> | |
<?php | |
if(!empty($_FILES)){ | |
$fileName = $_FILES["file"]["name"]; | |
$target_path = __DIR__."/thumbs/".$fileName; | |
$file_ext = pathinfo($fileName, PATHINFO_EXTENSION); | |
if(move_uploaded_file($_FILES["file"]["tmp_name"],$fileName)){ | |
/* read the source image */ | |
$desired_width = $_POST["desired_width"]; | |
switch($file_ext){ | |
case 'jpg': | |
$source_image = imagecreatefromjpeg($fileName); | |
break; | |
case 'jpeg': | |
$source_image = imagecreatefromjpeg($fileName); | |
break; | |
case 'png': | |
$source_image = imagecreatefrompng($fileName); | |
break; | |
default: | |
$source_image = imagecreatefrompng($fileName); | |
} | |
$width = imagesx($source_image); | |
$height = imagesy($source_image); | |
/* find the "desired height" of this thumbnail, relative to the desired width */ | |
$desired_height = floor($height * ($desired_width / $width)); | |
/* create a new, "virtual" image */ | |
$virtual_image = imagecreatetruecolor($desired_width, $desired_height); | |
/* copy source image at a resized size */ | |
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); | |
/* create the physical thumbnail image to its destination */ | |
switch($file_ext){ | |
case 'jpg' || 'jpeg': | |
imagejpeg($virtual_image,$target_path); | |
break; | |
case 'png': | |
imagepng($virtual_image,$target_path); | |
break; | |
default: | |
imagepng($virtual_image,$target_path); | |
} | |
unlink($fileName); | |
echo "thumb created"; | |
} | |
else{ | |
die("no"); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment