Skip to content

Instantly share code, notes, and snippets.

@carlosrenatohr
Created April 15, 2015 17:38
Show Gist options
  • Save carlosrenatohr/b7fa2e68ebfffca0ec7c to your computer and use it in GitHub Desktop.
Save carlosrenatohr/b7fa2e68ebfffca0ec7c to your computer and use it in GitHub Desktop.
Function to create thumbnail images with php
function createThumb($rawname, $ext, $path)
{
$custom_width = 200;
$type = str_replace('.', '', $ext);
$filename = $rawname . $ext;
$mini_filename = $rawname . '_mini' . $ext;
$file_path = $path . $filename;
// Build new image
if ($type == 'jpeg' || $type == 'jpg') {
$thumb = imagecreatefromjpeg($file_path);
} elseif ($type == 'png') {
$thumb = imagecreatefrompng($file_path);
} elseif ($type == 'gif') {
$thumb = imagecreatefromgif($file_path);
}
// Original sizes
$ox = imagesx($thumb);
$oy = imagesy($thumb);
// New sizes
$nx = $custom_width;
$ny = floor($oy * ($custom_width / $ox));
$nm = imagecreatetruecolor($nx, $ny);
// Creating thumbnail
imagecopyresized($nm, $thumb, 0, 0, 0, 0, $nx, $ny, $ox, $oy);
imagejpeg($nm, $path . $mini_filename);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment