php中的图像重命名函数

php中的图像重命名函数,php,image,resize,Php,Image,Resize,我有这个代码,它生成了两个图像 它所做的是生成名为small\u test.jpg.jpg和test.jpg.jpg 我需要生成的第一个名称是test\u.jpg,第二个名称是test.jpg 这需要对我需要帮助的函数进行更改 function setFile($src = null) { $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION)); if(is_file($src) && ($this-&

我有这个代码,它生成了两个图像

它所做的是生成名为
small\u test.jpg.jpg
test.jpg.jpg

我需要生成的第一个名称是
test\u.jpg
,第二个名称是
test.jpg

这需要对我需要帮助的函数进行更改

function setFile($src = null) {
  $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
  if(is_file($src) && ($this->ext == "JPG" OR $this->ext == "JPEG")) {
    $this->img_r = ImageCreateFromJPEG($src);
  } elseif(is_file($src) && $this->ext == "PNG") {
    $this->img_r = ImageCreateFromPNG($src);
  } elseif(is_file($src) && $this->ext == "GIF") {
    $this->img_r = ImageCreateFromGIF($src);
  }
  $this->img_w = imagesx($this->img_r);
  $this->img_h = imagesy($this->img_r);
}

function resize($largestSide = 100) {
  $width = imagesx($this->img_r);
  $height = imagesy($this->img_r);
  $newWidth = 0;
  $newHeight = 0;

  if($width > $height){
    $newWidth = $largestSide;
    $newHeight = $height * ($newWidth / $width);
  } else {
    $newHeight = $largestSide;
    $newWidth = $width * ($newHeight / $height);
  }

  $this->dst_r = ImageCreateTrueColor($newWidth, $newHeight);
  imagecopyresampled( $this->dst_r, 
                      $this->img_r, 
                      0, 0, 0, 0, 
                      $newWidth, $newHeight, 
                      $width, $height);
  $this->img_r = $this->dst_r;
  $this->img_h = $newHeight;
  $this->img_w = $newWidth;
}

function createFile($output_filename = null) {
  if($this->ext == "JPG" OR $this->ext == "JPEG") {
    imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext, $this->quality);
  } elseif($this->ext == "PNG") {
    imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
  } elseif($this->ext == "GIF") {
    imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
  }
  return $output_filename;
}

function setUploadDir($dirname) {
  $this->uploaddir = $dirname;
}

function flush() {
  $tempFile = $_FILES['Filedata']['tmp_name'];
  $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
  $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
  $filename = $_FILES['Filedata']['name']; 
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  $thumbnail = basename($filename,'.' .$ext) . '_s.' . $ext;    

  imagedestroy($this->dst_r);
  unlink($targetFile);
  imagedestroy($this->img_r);
}

}

$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
$filename = $_FILES['Filedata']['name']; 
$ext = pathinfo($FileName, PATHINFO_EXTENSION);
$thumbnail = basename($FileName,'.' .$ext) . '_s.' . $ext;  
move_uploaded_file ($tempFile, $targetFile);

$image = new Image();
$image->setFile($targetFile);
$image->setUploadDir($targetPath);
$image->resize(640);
$small_file = $image->createFile('small_'.$filename);
$image->resize(100);
$large_file = $image->createFile($filename);
$image->flush();
}

你的代码错得太厉害了,如果不完全重新编码,我就无法修复它

你应该试试看。它是我使用的一款,性能优良,无故障,安全可靠


它可以做很多事情,我强烈建议您使用它,而不是尝试创建自己的库。

这是很多代码。它到底在哪里不起作用?你得到了什么错误?在哪一条线上?输入是什么?请阅读这只是你想要更改的名称,还是没有上传?我没有收到任何错误,我只是不知道如何做我需要的。。。我的意思是,为了实现它,我知道我必须更改setFile和createFile函数,我只是不知道如何根据需要更改它。
$small\u file=$image->createFile('small.'$filename)在这里创建拇指。只需使用传递给函数的名称(
'small.$filename
)进行环绕播放即可。是。。我一直在看!我用的是我从uploadify上找到的东西,我只是看看你给我的代码,它实际上仍然不会给出输出@自从我意识到一个错误后,Al_12再次编辑。非常感谢你试图帮助我:)我很感激!我试过这个,但它没有创建图像…@Al_12是的,它只会按正确的路径发送,在一天结束时,您需要修复其中的错误,但结构是正确的。@Al_12实际上我会完全抛弃您的代码并使用:因为它更好