简体   繁体   English

php函数创建上传图片的缩略图

[英]php function to create thumb of uploaded image

i am created one function to upload image with random name and it worked properly. 我创建了一个函数来上传具有随机名称的图像,并且它可以正常工作。

function f_upload($file_name, $path)
{
    $ext=end(explode('.', $file_name['name']));

    $f_name=rand().time().'.'.$ext;

    if(move_uploaded_file($file_name['tmp_name'], $path.$f_name))
    {
        return $f_name; 
    }
    else
    {
        return false;
    }
}

now what i want is to create a thumb of same image with width of 75px on same directory but name start with t_imagename 现在我想要的是在同一目录上创建宽度为75px的相同图像的缩略图,但名称以t_imagename

i try this function but its giving error. 我尝试此功能,但它给错误。

function f_upload_gallary($file_name, $path)
{
    $ext=end(explode('.', $file_name['name']));

    $f_name=rand().time().'.'.$ext;

    if(move_uploaded_file($file_name['tmp_name'], $path.$f_name))
    {

            $src=$path.$f_name;
            $desired_width=75;
            $dest=$path;
            $source_image = imagecreatefromjpeg($src);
            $width = imagesx($source_image);
            $height = imagesy($source_image);

            $desired_height = floor($height*($desired_width/$width));
            $virtual_image = imagecreatetruecolor($desired_width,$desired_height);
            imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
            imagejpeg($virtual_image,$dest);


        return $f_name; 

    }
    else
    {
        return false;
    }
}

this function is not working. 此功能不起作用。

i just want to create a thumb of 75 on same folder with name start with t_ 我只想在名称为t_开头的同一文件夹上创建75的缩略图

Thanks 谢谢

You didn't specify the filename of destination image. 您未指定目标图片的文件名。 Replace this line, 替换此行,

$dest = $path;

with this: 有了这个:

$dest = $path.'t_'.$f_name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM