2017-04-17 10 views
1

私は複数のページの検証のプロジェクトに取り組んでいます。私の主な問題は、コードのアップロード部分です。私がやっていることは、ユーザーが画像をアップロードするときです。私はtempfolderでそれらを保存し、一度完全な一杯のフォーム、私はtempからmainfolderに画像を移動する前に、私はメインフォルダーに保存する前に画像のサイズを変更する前に。イメージをフォルダから移動し、別のフォルダに保存する前にサイズを変更するにはどうすればよいですか?

どうすれば実現できますか?

私は自分自身を試してみましたが、それをright.here得ることができませんでしたが、私のコードは

である私は、このエラーを得た: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

私はそれが私

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

前のイメージとしてそれを使用して、新しいイメージに

$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 
を救うために働く、非常に使いやすい、前にこのコードを持っていました
関連する問題