Reputation: 43
I'm trying to make it where it where when an image is uploaded it will compress the image into a smaller size than what it currently is or something like that. I have it where an admin is able to upload an image or images to the main page slider, but i'm wanting to make it where it's a smaller size of the image instead of the full 1.1mb image or what not. Any Ideas on how I can currently do this with my code?
Heres my Slider Code where it gets the link of where the image is from my DB:
<div class="tp-banner-container rev_slider_wrapper fullwidthbanner-container"
data-alias="news-hero72">
<div class="tp-banner-slider">
<ul>
<?php
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
if($stmt->rowCount() > 0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<li data-index="rs-80" data-transition="fade" data-slotamount="7" data-masterspeed="500"
data-saveperformance="on"
data-title="Intro Slide">
<img src="images/slider/<?php echo $row['link']; ?>" alt="slidebg1"
data-bgposition="center top" data-bgfit="cover"
data-bgrepeat="no-repeat">
<div class="revolution_heading_font tp-caption grey_heavy_72 skewfromrightshort tp-resizeme rs-parallaxlevel-2" data-x="0"
data-y="200" data-speed="700" data-start="10" data-easing="Power3.easeInOut" data-splitin="chars"
data-splitout="none" data-elementdelay="0.1" data-endelementdelay="0.1"
style="z-index: 5; max-width: inherit; max-height: inherit; white-space: nowrap;color: #FFFFFF;font-size:30pt;font-family: Montserrat">
<?php echo $row['slide_name']; ?>
</div>
<div class="revolution_font tp-caption grey_heavy_72 skewfromrightshort tp-resizeme rs-parallaxlevel-2" data-x="5"
data-y="300" data-speed="300" data-start="600" data-easing="Power3.easeInOut" data-splitin="words"
data-splitout="none" data-elementdelay="0.1" data-endelementdelay="0.1"
style="z-index: 5; max-width: inherit; max-height: inherit; white-space: nowrap;color: #ffffff;font-weight: 300;font-size:18pt; line-height: 30pt; margin-left:8px">
<?php echo $row['slide_desc'];?>
</div>
<?php
}
}
else
{
?>
<?php
}
?>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
Here's my PHP Where the admin is able to edit the slider name, description and upload slider images.
<?php
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
$id = $_GET['edit_id'];
$stmt_edit = $DB_con->prepare('SELECT * FROM sponsors WHERE id =:uid');
$stmt_edit->execute(array(':uid'=>$id));
$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
extract($edit_row);
}
else
{
header("Location: ../../login.php");
}
if(isset($_POST['btn_save_updates']))
{
$username = $_POST['user_name'];
$description = $_POST['description'];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
if($imgFile)
{
$upload_dir = '../images/sponsors/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userprofile = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions))
{
if($imgSize < 5000000)
{
unlink($upload_dir.$edit_row['logo']);
move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
}
else
{
$errMSG = "Sorry, Your File Is Too Large To Upload. It Should Be Less Than 5MB.";
}
}
else
{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF Extension Files Are Allowed.";
}
}
else
{
$userprofile = $edit_row['logo'];
}
if(!isset($errMSG))
{
$sponsorname = $_POST['sponsor_name'];
$motto = $_POST['sponsor_motto'];
$phone = $_POST['sponsor_phone'];
$website = $_POST['sponsor_website'];
$son = $_POST['sponsor_on'];
$stmt = $DB_con->prepare('UPDATE sponsors SET name=:sname, motto=:smotto, phone=:sphone,website=:swebsite,live=:son, logo=:upic WHERE id=:uid');
$stmt->bindParam(':sname',$sponsorname);
$stmt->bindParam(':smotto',$motto);
$stmt->bindParam(':sphone',$phone);
$stmt->bindParam(':swebsite',$website);
$stmt->bindParam(':son',$son);
$stmt->bindParam(':upic',$userprofile);
$stmt->bindParam(':uid',$id);
if($stmt->execute()){
?>
<script>
alert('Successfully Updated...');
window.location.href='managesponsors.php?action=sponsorupdated';
</script>
<?php
}
else{
$errMSG = "Sorry User Could Not Be Updated!";
}
}
}
?>
Upvotes: 0
Views: 301
Reputation: 676
If what you want is to compress the uploaded image file then the straight forward way is to use php function imagejpeg() and edit your upload code from this:
move_uploaded_file($tmp_dir,$upload_dir.$userprofile);
to this:
if (move_uploaded_file($tmp_dir,$upload_dir.$userprofile)) {
$image = imagecreatefromjpeg($upload_dir.$userprofile);
imagejpeg($image,$upload_dir.$userprofile,75); //75 is quality of the compression (can be anywhere lower than 100)
}
Now your image should be 75% of the original size, do note that this only works for jpeg images, you will need to replace imagecreatefromjpeg with imagecreatefromgif and imagecreatefrompng depending on the uploaded image format
Upvotes: 2
Reputation: 680
you could use either GD or Imagick to accomplish this like below
$thumb = new Imagick('myimage.gif');
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
$thumb->destroy();
or you will get plenty of PHP libraries for the same which will ease your job.
Upvotes: 1