Need a bit of a checking on this code please

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
benoit1980
New php-forum User
New php-forum User
Posts: 23
Joined: Wed Apr 27, 2016 12:18 pm

Fri May 06, 2016 5:26 am

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

Code: Select all

///////////////////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>




benoit1980
New php-forum User
New php-forum User
Posts: 23
Joined: Wed Apr 27, 2016 12:18 pm

Fri May 06, 2016 5:30 am

Well just noticed that the file name is written correctly in the database and the file is upload to the upload location correctly too but the image is still 2000px by 1000px.....

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 07, 2016 2:14 am

Code: Select all

function resize($width, $height){
  $image_resized = $width.'x'.$height.'_'.$_FILES['image']['name'];

  //Nothing changes $image_resized

  return $image_resized;
}

benoit1980
New php-forum User
New php-forum User
Posts: 23
Joined: Wed Apr 27, 2016 12:18 pm

Sat May 07, 2016 6:40 am

Thanks for the reply.

I would like to ask you, I have seen functions on the internet where images are returned as a string, any idea why would someone do that? Is it actually possible to save an image as a string and reconvert it as an image? If this is the case then having a string should be far smaller than uploading a full image no?

Thanks,

Ben

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 07, 2016 8:17 am

A string or an image is represented in memory as numbers, the lump of memory containing your data also has some sort of header or an index to it which tells the interpreter how that memory should be accessed - string, integer, image etc..

benoit1980
New php-forum User
New php-forum User
Posts: 23
Joined: Wed Apr 27, 2016 12:18 pm

Sat May 07, 2016 12:41 pm

Thank you for the reply but what would take the most space? To save the image as a string in the database or upload it as an image and save it´s path + name?

Thank you.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 07, 2016 12:58 pm

I don't know, but would speculate that there is little if any difference.

If you really want to know then you can do a test to see which would take up the least / most room.

However; I would keep to image references to keep the code simple, readable and easier to maintain.

Post Reply