我正在开展多页验证的项目。我的主要问题是代码的上传部分。我正在做的是当用户上传他的图像时;我将它们保存在一个临时文件夹中,然后一旦表格填满,我将图像从临时文件夹移动到主文件夹,但在我想要在将图像保存到主文件夹之前调整大小之前。
我如何实现这一目标?
我已经尝试过自己,但无法做到正确。我的代码是
我收到此错误:getimagesize(174349.jpg): failed to open stream: No such file or directory
imageuploaded.php
<?php
define('MAX_FILE_SIZE', 952070);
define('UPLOAD_DIR_TEMP', BASE_URI.'html/temp/');
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg','image/png');
if (array_key_exists('upload', $_POST)) {
foreach ($_FILES['image']['name'] as $number => $file) {
// replace any spaces in the filename with underscores
$file = str_replace(' ','_',$file);
$fileNom = pathinfo($_FILES['image']["name"][$number], PATHINFO_FILENAME);
$ext = pathinfo($_FILES['image']["name"][$number],PATHINFO_EXTENSION);
//$soruceimg = $_FILES['image']['tmp_name'];
//echo $file;
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type'][$number]) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error'][$number]) {
case 0:
// get the date and time
ini_set('date.timezone', 'Africa/Abidjan');
$now = date('Y-m-d-His');
//$Nfilename = basename(substr($file,0,6).$now).".".$ext ." ";
$successes = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR_TEMP.$file);
if ($successes) {
$_SESSION['imgname']= $_FILES['image']['name'];
header('Location: test.php');
//echo $file;
}
else {
$resultfile[] = "Error uploading $file. Please try again.";
}
break;
case 3:
$resultfile[] = "Error uploading $file. Please try again.";
default:
$resultfile[] = "System error uploading $file. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'][$number] == 4) {
$errNofile[] = 1;
}
else {
$resultfile[] = "$file cannot be uploaded. Maximum size: $max.Acceptable file types: gif, jpg, png.";
}
}
}
imageresize.php
$source = BASE_URI.'html/temp/';
define('THUMBS_DIR', BASE_URI.'html/products/thumbs/');
define('MAX_WIDTH', 200);
define('MAX_HEIGHT', 200);
define('UPLOAD_DIR', BASE_URI.'html/products/');
ini_set('date.timezone', 'Africa/Abidjan');
$now = date('Y-m-d-His');
if (isset($_SESSION['imgname']) && is_array($_SESSION['imgname']) ) {
foreach ($_SESSION['imgname'] as $files) {
echo $files;
$img = str_replace(" ", "_", $files);
$ext = pathinfo($img,PATHINFO_EXTENSION);
if (file_exists($source.$img)) {
$ext = pathinfo($img,PATHINFO_EXTENSION);
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename(substr($img,0,6).$now));
list($width, $height, $type) = getimagesize($img);
if ($width != $height) {
echo "file width and height must be same";
break;
}
elseif ( $width < 300 && $height < 300) {
echo "File must be at least 400 x 400";
break;
}
else{
$ratio = 1;
$ratio = MAX_WIDTH/$width;
}
$Nfilename = $name.'.'.$ext;
$moved = rename($source.$img, UPLOAD_DIR.$Nfilename);
if (!$moved) {
echo "Problem moving file".$img;
}
else{
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {
$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
switch($type) {
case 1:
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
$thumb_name = $name.'_thb.jpg';
}
break;
case 2:
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
$thumb_name = $name.'_thb.jpg';
break;
case 3:
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
}
if ($success) {
$result = "$thumb_name created";
}
else {
$result = 'Problem creating thumbnail';
}
if ($success) {
echo $thumb_name;
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
}
}
else{
echo "No file exist";
}
}
}
答案 0 :(得分:0)
首先将图像转换为位图,然后使用定义比率的位图压缩来压缩大小。 您也可以通过声明特定的高度和宽度来压缩图像。
答案 1 :(得分:0)
之前我有过这段代码,非常好用,对我有用
class SimpleImage {
var $image; var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); }
elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); }
elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); }
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); }
elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); }
elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); }
if( $permissions != null) { chmod($filename,$permissions); }
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); }
elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); }
elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); }
}
function getWidth() { return imagesx($this->image); }
function getHeight() { return imagesy($this->image); }
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio; $this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio; $this->resize($width,$height);
}
function scale($scale) { $width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100; $this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image; }
}
将其用作prev image,并保存新图像
$imageRes = new SimpleImage();
$imageRes->load($_SERVER['DOCUMENT_ROOT'] . '/upload/' . $image . '.jpg');//prev image
$imageRes->resizeToWidth(120);
$imageRes->save($_SERVER['DOCUMENT_ROOT'] . '/upload/' . $image . '.jpg');//new image