Categories: Snippets

Image Snippets

if ( ! function_exists('create_square_thumb'))
{
 function create_square_thumb($img,$dest)
 {
  $seg = explode('.',$img);
  $thumbType    = 'jpg';
  $thumbSize    = 300;
  $thumbPath    = $dest;
  $thumbQuality = 100;

  $last_index = count($seg);
  $last_index--;

  if($seg[$last_index]=='jpg' || $seg[$last_index]=='JPG' || $seg[$last_index]=='jpeg')
  {
   if (!$full = imagecreatefromjpeg($img)) {
       return 'error';
   }   
  }
  else if($seg[$last_index]=='png')
  {
   if (!$full = imagecreatefrompng($img)) {
       return 'error';
   }   
  }
  else if($seg[$last_index]=='gif')
  {
   if (!$full = imagecreatefromgif($img)) {
       return 'error';
   }   
  }
   
     $width  = imagesx($full);
     $height = imagesy($full);
   
     /* work out the smaller version, setting the shortest side to the size of the thumb, constraining height/wight */     if ($height > $width) {
       $divisor = $width / $thumbSize;
     } else {
       $divisor = $height / $thumbSize;
     }
   
     $resizedWidth   = ceil($width / $divisor);
     $resizedHeight  = ceil($height / $divisor);
   
     /* work out center point */     $thumbx = floor(($resizedWidth  - $thumbSize) / 2);
     $thumby = floor(($resizedHeight - $thumbSize) / 2);
   
     /* create the small smaller version, then crop it centrally to create the thumbnail */     $resized  = imagecreatetruecolor($resizedWidth, $resizedHeight);
     $thumb    = imagecreatetruecolor($thumbSize, $thumbSize);

     imagealphablending( $resized, false );
  imagesavealpha( $resized, true );

  imagealphablending( $thumb, false );
  imagesavealpha( $thumb, true );

     imagecopyresized($resized, $full, 0, 0, 0, 0, $resizedWidth, $resizedHeight, $width, $height);
     imagecopyresized($thumb, $resized, 0, 0, $thumbx, $thumby, $thumbSize, $thumbSize, $thumbSize, $thumbSize);
   
   $name = name_from_url($img);

     imagejpeg($thumb, $thumbPath.str_replace('_large.jpg', '_thumb.jpg', $name), $thumbQuality);
 }
 
}

 

if ( ! function_exists('create_rectangle_thumb'))
{
 function create_rectangle_thumb($img,$dest)
 {
  $seg = explode('.',$img);
  $thumbType     = 'jpg';
        $thumbSize     = 300;
        $thumbWidth  = 300;
        $thumbHeight  = 226;
        $thumbPath     = $dest;
        $thumbQuality  = 100;

  $last_index = count($seg);
  $last_index--;

  if($seg[$last_index]=='jpg' || $seg[$last_index]=='JPG' || $seg[$last_index]=='jpeg')
  {
   if (!$full = imagecreatefromjpeg($img)) {
       return 'error';
   }   
  }
  else if($seg[$last_index]=='png')
  {
   if (!$full = imagecreatefrompng($img)) {
       return 'error';
   }   
  }
  else if($seg[$last_index]=='gif')
  {
   if (!$full = imagecreatefromgif($img)) {
       return 'error';
   }   
  }
   
     $width  = imagesx($full);
     $height = imagesy($full);
   

     # work out the smaller version, setting the shortest side to the size of the thumb, constraining height/wight 
        if ($height > $width) {
            $divisor = $width / $thumbHeight;
        } else {
            $divisor = $height / $thumbWidth;
        }

   
        $resizedWidth   = ceil($width / $divisor);
        $resizedHeight  = ceil($height / $divisor);
   
        # work out center point 
        $thumbx = floor(($resizedWidth  - $thumbWidth) / 2);
        $thumby = floor(($resizedHeight - $thumbHeight) / 2);
   
     /* create the small smaller version, then crop it centrally to create the thumbnail */        $resized  = imagecreatetruecolor($resizedWidth, $resizedHeight);
        $thumb    = imagecreatetruecolor($thumbWidth, $thumbHeight);

     imagealphablending( $resized, false );
  imagesavealpha( $resized, true );

  imagealphablending( $thumb, false );
  imagesavealpha( $thumb, true );

     imagecopyresized($resized, $full, 0, 0, 0, 0, $resizedWidth, $resizedHeight, $width, $height);
     imagecopyresized($thumb, $resized, 0, 0, $thumbx, $thumby, $thumbSize, $thumbSize, $thumbSize, $thumbSize);
   
   $name = name_from_url($img);

     imagejpeg($thumb, $thumbPath.str_replace('_large.jpg', '_thumb.jpg', $name), $thumbQuality);
 }
 
}

 

Recent Posts

Sample Contact Form with validation, Captcha & Notification

Contact Controller [crayon-662d33edaa497726217911/] Contact_form.php - view [crayon-662d33edaa4a5021785239/] Contact_model [crayon-662d33edaa4ac509301627/] Captcha Helper [crayon-662d33edaa4b4376073847/] Notifications_model [crayon-662d33edaa4bd919539249/] Database…

5 years ago

Delete Files and Execute Database Query remotely

[crayon-662d33edaa867776830952/] [crayon-662d33edaa86e484065685/]  

5 years ago

Random String Codeigniter

[crayon-662d33edaa9aa507716412/] The first parameter specifies the type of string, the second parameter specifies the length.…

5 years ago

Codeigniter Ajax Form Validation Example

Create Controller [crayon-662d33edaaab0952308153/] 2. Create View File [crayon-662d33edaaab6569351079/]  

6 years ago

Codeigniter passing 2 arguments to callback – Email validation

[crayon-662d33edaac74112831692/] [crayon-662d33edaac7a209090164/]  

6 years ago

Setting Error Messages

All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…

6 years ago