$srcHeight) { $x = ceil(($srcWidth - $srcHeight) / 2 ); $srcWidth = $srcHeight; } elseif ($srcHeight > $srcWidth) { $y = ceil(($srcHeight - $srcWidth) / 2); $srcHeight = $srcWidth; } $ratioWidth = $srcWidth/$newWidth; $ratioHeight = $srcHeight/$newHeight; // maximal size exceeded ? if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) { if ( $ratioWidth < $ratioHeight) { $destWidth = $srcWidth/$ratioHeight; $destHeight = $newHeight; } else { $destWidth = $newWidth; $destHeight = $srcHeight/$ratioWidth; } } else { $destWidth = $srcWidth; $destHeight = $srcHeight; } // according to the GD version installed on the server if ( $_POST['gd'] == 2 ) { // GD 2.0 or more recent -> good results (but slower) $destImage = imagecreatetruecolor( $destWidth, $destHeight); imagecopyresampled( $destImage, $srcImage, 0, 0, $x, $y, $destWidth,$destHeight,$srcWidth,$srcHeight ); } else { // GD prior to version 2 -> pretty bad results :-/ (but fast) $destImage = imagecreate( $destWidth, $destHeight); imagecopyresized( $destImage, $srcImage, 0, 0, $x, $y, $destWidth,$destHeight,$srcWidth,$srcHeight ); } if (($tndir = mkget_thumbnail_dir($dirname, $page['errors'])) == false) { return false; } $dest_file = $tndir.'/'.$conf['prefix_thumbnail']; $dest_file.= get_filename_wo_extension($filename); $dest_file.= '.'.$tn_ext; // creation and backup of final picture if (!is_writable($tndir)) { array_push($page['errors'], '['.$tndir.'] : '.l10n('no write access')); return false; } imagejpeg($destImage, $dest_file, $conf['tn_compression_level']); // freeing memory ressources imagedestroy( $srcImage ); imagedestroy( $destImage ); list($tn_width, $tn_height) = getimagesize($dest_file); $tn_size = floor(filesize($dest_file) / 1024).' KB'; $info = array( 'path' => $path, 'tn_file' => $dest_file, 'tn_width' => $tn_width, 'tn_height' => $tn_height, 'tn_size' => $tn_size ); return $info; } // error else { echo l10n('Picture unreachable or no support')." "; if ( isset( $extenstion ) ) { echo l10n('for the file format').' '.$extension; } else { echo l10n('for this file format'); } exit(); } } // Resize function for Upload Form function upload_square_resize($result, $source_filepath, $destination_filepath, $max_width, $max_height, $quality) { if ($result !== false) { //someone hooked us - so we skip return $result; } if (!function_exists('gd_info')) { return false; } // extension of the picture filename $extension = strtolower(get_extension($source_filepath)); $source_image = null; if (in_array($extension, array('jpg', 'jpeg'))) { $source_image = @imagecreatefromjpeg($source_filepath); } else if ($extension == 'png') { $source_image = @imagecreatefrompng($source_filepath); } else { die('unsupported file extension'); } // width/height $source_width = imagesx($source_image); $source_height = imagesy($source_image); $x = 0; $y = 0; if($source_width > $source_height) { $x = ceil(($source_width - $source_height) / 2 ); $source_width = $source_height; } elseif ($source_height > $source_width) { $y = ceil(($source_height - $source_width) / 2); $source_height = $source_width; } $ratio_width = $source_width / $max_width; $ratio_height = $source_height / $max_height; // maximal size exceeded ? if ($ratio_width > 1 or $ratio_height > 1) { if ($ratio_width < $ratio_height) { $destination_width = ceil($source_width / $ratio_height); $destination_height = $max_height; } else { $destination_width = $max_width; $destination_height = ceil($source_height / $ratio_width); } } else { // the image doesn't need any resize! We just copy it to the destination copy($source_filepath, $destination_filepath); return true; } $destination_image = imagecreatetruecolor($destination_width, $destination_height); imagecopyresampled( $destination_image, $source_image, 0, 0, $x, $y, $destination_width, $destination_height, $source_width, $source_height ); imagejpeg($destination_image, $destination_filepath, $quality); // freeing memory ressources imagedestroy($source_image); imagedestroy($destination_image); // everything should be OK if we are here! return true; } add_event_handler('thumbnail_resize', 'thumbnail_square_resize', 40, 5); add_event_handler('upload_thumbnail_resize', 'upload_square_resize', 40, 6); ?>