A
Anonymous
Guest
Hello,
My code is working as I have just tested it on my website, I need to resize a lot of images on my website and before I start implementing this all over, I would like to know if my code seems correct to you, I just need a little feedback to make sure I have coded it correctly please.
I am still on the php learning curve....so there are perhaps a lot of mistakes in there which I do not see.
Thanks in Advance
My code is working as I have just tested it on my website, I need to resize a lot of images on my website and before I start implementing this all over, I would like to know if my code seems correct to you, I just need a little feedback to make sure I have coded it correctly please.
I am still on the php learning curve....so there are perhaps a lot of mistakes in there which I do not see.
Thanks in Advance
Code:
///////////////////FUNCTION TO ADD IMAGE
function add_slide(){
if(isset($_POST['add_slide'])){
$slide_title = escape_string($_POST['slide_title']);
$slide_image = escape_string($_FILES['image']['name']);
$slide_image_loc = escape_string($_FILES['image']['tmp_name']);
//$slide_sizes = array(100 => 100, 150 => 150, 250 => 250);
$max_file_size = 1024*200; // 200kb Max
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
if(empty($slide_title) || empty($slide_image)){
echo "<p class='bg-danger'>This field cannot be empty</p>";
}elseif($_FILES['image']['size'] < $max_file_size ){
$get_extension = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
if(in_array($get_extension, $valid_extensions)) {
$image_resized = resize(250, 250);
move_uploaded_file($slide_image_loc, UPLOAD_DIRECTORY . DS . $image_resized);
$query = query("INSERT INTO slides(slide_title, slide_image) VALUES ('{$slide_title}','{$image_resized}') ");
confirm($query);
set_message("Slide Added!");
redirect("index.php?slides");
} else {
$msg = 'Unsupported file';
}
}
}
}
////////////////////////////resize function///////////////////////////////////
function resize($width, $height){
/* Get original image x y*/
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* new file name */
$image_resized = $width.'x'.$height.'_'.$_FILES['image']['name'];
/* read binary data from image file */
$imgString = file_get_contents($_FILES['image']['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
return $image_resized;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
//HTML FORM
<?php add_slide(); ?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="image">
</div>
<div class="form-group">
<label for="title">Slide Title</label>
<input type="text" name="slide_title" class="form-control">
</div>
<div class="form-group">
<input type="submit" name="add_slide">
</div>
</form>