function create_thumbnail($filename,$fileext,$directory) { $max_w = '400'; $max_h = '400'; $sTempFileName = $directory.'/'.$filename.'.'.$fileext; $dest_thumbnail = $directory.'/'.$filename.'_thumbnail.'.$fileext; $oTempFile = fopen($sTempFileName, "r"); $sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName)); $oSourceImage = imagecreatefromstring($sBinaryPhoto); $nWidth = imagesx($oSourceImage); // get original source image width $nHeight = imagesy($oSourceImage); // and height $height = $nHeight; $width = $nWidth; $ratio=$height/$width; // resize image? if ($width > $max_w || $height > $max_h) { // resize by height, then width (height dominant) if ($max_h < $max_w) { $rs_h = $max_h; $rs_w = $rs_h / $ratio; } // resize by width, then height (width dominant) else { $rs_w = $max_w; $rs_h = $ratio * $rs_w; } } // image requires no resizing else { $rs_w = $width; $rs_h = $height; } $nDestinationWidth = $rs_w; $nDestinationHeight = $rs_h; $oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight); imagecopyresized( $oDestinationImage, $oSourceImage, 0, 0, 0, 0, $nDestinationWidth, $nDestinationHeight, $nWidth, $nHeight); // resize the image switch($fileext) { case 'jpg': imageJPEG($oDestinationImage,$dest_thumbnail,100); break; case 'jpeg': imageJPEG($oDestinationImage,$dest_thumbnail,100); break; case 'gif': imageGIF($oDestinationImage,$dest_thumbnail,100); break; case 'png': imagePNG($oDestinationImage,$dest_thumbnail,100); break; } if(is_file($dest_thumbnail)) return $filename.'_thumbnail.'.$fileext; else return ''; }