An example of a picture upload
Last Update:2017-02-28
Source: Internet
Author: User
Upload PHP Code:--------------------------------------------------------------------------------
?
$FILENAME = "Image_name";
Build the width of the picture
$RESIZEWIDTH = 400;
Height of the build picture
$RESIZEHEIGHT = 400;
function Resizeimage ($im, $maxwidth, $maxheight, $name) {
$width = Imagesx ($im);
$height = Imagesy ($im);
if ($maxwidth && $width > $maxwidth) | | ($maxheight && $height > $maxheight)) {
if ($maxwidth && $width > $maxwidth) {
$widthratio = $maxwidth/$width;
$RESIZEWIDTH =true;
}
if ($maxheight && $height > $maxheight) {
$heightratio = $maxheight/$height;
$RESIZEHEIGHT =true;
}
if ($RESIZEWIDTH && $RESIZEHEIGHT) {
if ($widthratio < $heightratio) {
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif ($RESIZEWIDTH) {
$ratio = $widthratio;
}elseif ($RESIZEHEIGHT) {
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if (function_exists ("imagecopyresampled")) {
$newim = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = Imagecreate ($newwidth, $newheight);
Imagecopyresized ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
Imagejpeg ($newim, $name. ". jpg");
Imagedestroy ($newim);
}else{
Imagejpeg ($im, $name. ". jpg");
}
}
if ($_files[' image '] [' size ']) {
if ($_files[' image '] [' type '] = = "Image/pjpeg") {
$im = imagecreatefromjpeg ($_files[' image '] [' tmp_name ']);
}elseif ($_files[' image '] [' type '] = = "Image/x-png") {
$im = imagecreatefrompng ($_files[' image '] [' tmp_name ']);
}elseif ($_files[' image '] [' type '] = = "Image/gif") {
$im = imagecreatefromgif ($_files[' image '] [' tmp_name ']);
}
if ($im) {
if (File_exists ("$FILENAME. jpg")) {
Unlink ("$FILENAME. jpg");
}
Resizeimage ($im, $RESIZEWIDTH, $RESIZEHEIGHT, $FILENAME);
Imagedestroy ($im);
}
}
?>
<br><br>
<form enctype= "Multipart/form-data" method= "POST" >
<br>
<input type= "file" name= "image" Size= "a" value= "browse" ><p>
<input type= "Submit" value= "Upload picture" >
</form>
</body>