简体   繁体   English

PHP中的图像重命名功能

[英]image rename function in php

I have this code which generates me two images. 我有这段代码可以生成两个图像。

What it does is generate images with the names small_test.jpg.JPG and test.jpg.JPG 它的作用是产生与名称图像small_test.jpg.JPGtest.jpg.JPG

What I need is for the first name generated to be test_s.jpg and the second one to just be test.jpg . 我需要的是用于产生相应的第一名称test_s.jpg ,第二个到只是test.jpg

This requires changes in the functions that I need help with. 这需要对我需要帮助的功能进行更改。

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();
}

Your code is so wrong I cannot fix it without recoding it completely. 您的代码太错误了,如果不重新编码就无法修复。

You should give: http://phpthumb.gxdlabs.com a try. 您应该尝试: http : //phpthumb.gxdlabs.com It is the one I use and it is performant, bugless and security aware. 我使用的是它,它性能好,无错误且具有安全意识。

It can do a lot of stuff and I would strongly recommend using this instead of trying to make your own library. 它可以做很多事情,我强烈建议您使用它,而不要尝试创建自己的库。

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

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