使用PHP为不同的Internet格式创建Tumbnail

使用PHP为不同的Internet格式创建Tumbnail,php,gd,Php,Gd,所以我做了一个脚本,用php动态创建tumbnails,它工作得很好,但是我创建tumbnails的这些文件是从用户上传的。我有足够的验证来知道上传的是哪种类型的图像。允许的一次是(JPG PNG GIF)没有我在上面创建了这个脚本 if (pathinfo($fileName,PATHINFO_EXTENSION) == 'png'){ $img = imagecreatefrompng($basePath.'/'.$fileName);

所以我做了一个脚本,用php动态创建tumbnails,它工作得很好,但是我创建tumbnails的这些文件是从用户上传的。我有足够的验证来知道上传的是哪种类型的图像。允许的一次是(JPG PNG GIF)没有我在上面创建了这个脚本

if (pathinfo($fileName,PATHINFO_EXTENSION) == 'png'){
                $img = imagecreatefrompng($basePath.'/'.$fileName);
                $width = imagesx($img);
                $height = imagesx($img);

                //Calculateing tumbnails size
                $newWidth = 100;
                $newHeight = floor($height*(100/$width));

                //Create a new tempoary image
                $tmpImage = imagecreatetruecolor($newWidth,$newHeight);

                imagecopyresized($tmpImage,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);

                imagepng($tmpImage,$pathToTumb.'/'.$fileName);
            }
            if (pathinfo($fileName,PATHINFO_EXTENSION) == 'jpg'){
                $img = imagecreatefromjpeg($basePath.'/'.$fileName);
                $width = imagesx($img);
                $height = imagesx($img);

                //Calculateing tumbnails size
                $newWidth = 100;
                $newHeight = floor($height*(100/$width));

                //Create a new tempoary image
                $tmpImage = imagecreatetruecolor($newWidth,$newHeight);

                imagecopyresized($tmpImage,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);

                imagejpeg($tmpImage,$pathToTumb.'/'.$fileName);
            }
            if(pathinfo($fileName,PATHINFO_EXTENSION) == 'gif'){
                $img = imagecreatefromgif($basePath.'/'.$fileName);
                $width = imagesx($img);
                $height = imagesx($img);

                //Calculateing tumbnails size
                $newWidth = 100;
                $newHeight = floor($height*(100/$width));

                //Create a new tempoary image
                $tmpImage = imagecreatetruecolor($newWidth,$newHeight);

                imagecopyresized($tmpImage,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);

                imagegif($tmpImage,$pathToTumb.'/'.$fileName);
            }
我发现非常重复,对于不同的格式有不同的方法吗


此外,我还想知道,如果图像没有扩展名,是否可以执行此操作,例如,如果文件名为just test而不是test.jpg

如果您发现自己复制并粘贴代码,则应将该代码转换为函数。您的代码可以重构如下:

switch (pathinfo($fileName, PATHINFO_EXTENSION)) {
    case 'png':
        $tmpImage = thumb(imagecreatefrompng($basePath.'/'.$fileName));
        imagepng($tmpImage,$pathToTumb.'/'.$fileName);
        break;
    case 'jpg':
        $tmpImage = thumb(imagecreatefromjpeg($basePath.'/'.$fileName));
        imagejpeg($tmpImage,$pathToTumb.'/'.$fileName);
        break;
    case 'gif':
        $tmpImage = thumb(imagecreatefromgif($basePath.'/'.$fileName));
        imagegif($tmpImage,$pathToTumb.'/'.$fileName);
        break;
}

function thumb($img) {
    $width = imagesx($img);
    $height = imagesx($img);

    //Calculateing tumbnails size
    $newWidth = 100;
    $newHeight = floor($height*(100/$width));

    //Create a new tempoary image
    $tmpImage = imagecreatetruecolor($newWidth,$newHeight);

    imagecopyresized($tmpImage,$img,0,0,0,0,$newWidth,$newHeight,$width,$height);

    return $tmpImage;
}

用于检测没有文件扩展名的图像类型,您可以使用,或者,如果您的主机上没有文件扩展名,则可以使用。

这些功能非常有效!谢谢你,我稍后会试试扩展的东西,谢谢你的帮助。