2

这是上传用户徽标的代码,我想知道是否有人可以帮助我。我也希望能够允许用户上传 .png 和 .gif 文件。

对不起,如果这是一个简单的修复,但我对 php 很陌生。

<?
//edit logo

function resize_image ($image) {
    $imgsize = getimagesize($image);
    //check for gallery type to determine thumbnail size

    $size_x = 150;      
    $ratio = 150 / $imgsize[0];
    $size_y = $imgsize[1] * $ratio;


    $srcimage         = ImageCreateFromjpeg ($image);
    $newimage         = ImageCreateTrueColor($size_x,$size_y);  
    //$newimage        = imagecreate ($size_x, $size_y);
    imagecopyresized ($newimage, $srcimage, 0, 0, 0, 0, $size_x, $size_y,
    imagesx($srcimage), imagesy($srcimage));
    return $newimage;
}

$myrepairer = new repairer;


//resize and upload image

$file =  $_FILES['logo']['tmp_name'];
$file_name =  $_FILES['logo']['name'];

//upload the image followed by a db update.
if ($file != 'none') {
if (copy($file,'logos/'.$_REQUEST['id'].'.jpg')) {
    unlink($file);
}
$myrepairer->updatelogo($_REQUEST['id'],$_REQUEST['id']);   
$thumbnail = resize_image('logos/'.$_REQUEST['id'].'.jpg');
unlink('logos/'.$_REQUEST['id'].'.jpg');
ImageJPEG($thumbnail,'logos/'.$_REQUEST['id'].'.jpg');
}

$resultmessage = '<div align="center" class="GreenText">Logo Updated</div>';
4

1 回答 1

1

您可以获取文件扩展名并使用它而不是“.jpg”。

$ext = pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION);

所以,它会是这样的:

$ext = pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION);

//upload the image followed by a db update.
if ($file != 'none') {
if (copy($file,'logos/'.$_REQUEST['id'].'.'.$ext)) {
    unlink($file);
}
$myrepairer->updatelogo($_REQUEST['id'],$_REQUEST['id']);   
$thumbnail = resize_image('logos/'.$_REQUEST['id'].'.'.$ext);
unlink('logos/'.$_REQUEST['id'].'.'.$ext);
ImageJPEG($thumbnail,'logos/'.$_REQUEST['id'].'.'.$ext);
于 2013-02-25T17:12:59.367 回答