I use this function for resize and upload image.
code:
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
function createThumbnail($widthbig,$widththumb,$image_directory,$image_directory_thumb,$newfilename,$newfilenamethumb,$source) {
define ("MAX_SIZE","5000");
$errors=0;
//$image =$_FILES["post_images"]["name"];
//$uploadedfile = $_FILES['file']['tmp_name'];
if ($newfilename)
{
$file = stripslashes($newfilename);
$extension = $this->getExtension($file);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo ' Unknown Image extension ';
$errors=1;
}
else
{
$size= filesize($source);
if ($size > MAX_SIZE*1024)
{
echo "You have exceeded the size limit";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
//$source = $source;
$src = imagecreatefromjpeg($source);
}
else if($extension=="png")
{
//$uploadedfile = $source;
$src = imagecreatefrompng($source);
}
else
{
$src = imagecreatefromgif($source);
}
list($width,$height)=getimagesize($source);
$newwidth=$widthbig;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=$widththumb;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,
$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,
$width,$height);
$dirfile = $image_directory. $newfilename;
$dirfile1 = $image_directory_thumb. $newfilenamethumb;
if($extension=="jpg" || $extension=="jpeg" )
{
$upload = imagejpeg($tmp,$dirfile,100);
$upload1 = imagejpeg($tmp1,$dirfile1,100);
}
else if($extension=="png")
{
$upload = imagepng($tmp,$dirfile,100);
$upload1 = imagepng($tmp1,$dirfile1,100);
}
else if($extension=="gif")
{
$upload = imagegif($tmp,$dirfile,100);
$upload1 = imagegif($tmp1,$dirfile1,100);
}
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
before, this function work in php 5.x but when in 7 this not work and give return null after i var_dump the imagejpg("xx","xx","xx"); end the result is false.
I have try to search in google but not get the same problem.
whats wrong whit this code, and how to fix this?
thanks
I was fix this : can't upload because permission denied in target folder
change to
chmod -r 0757 target_dir
Related
In the form and process file below I am trying to upload 3 sizes of multiple images upload and it is uploading 3 size perfectly fine.
But it is uploading the same image on all sizes of the last image selected.
UPDATE
What i have observed that something has to be played with $src i tried below and when i do this images saved black. $src does not accept [$Kv]
foreach($_FILES['file']['tmp_name'] as $src[$Kv]) {
if($extension[$Kv]=="jpg" || $extension[$Kv]=="jpeg" ){
$uploadedfile[$Kv] = $_FILES['file']['tmp_name'][$Kv];
$src[$Kv] = imagecreatefromjpeg($uploadedfile[$Kv]);
}
list($width,$height)=getimagesize($uploadedfile[$Kv]);
////// 1st Size of Image
$newwidth=350;
$newheight=350;
$tmp[$Kv]=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp[$Kv],$src[$Kv],0,0,0,0,$newwidth,$newheight,$width,$height);
}
.
Suppose I selected 3 images to upload.
Image A
Image B
Image C
It is converting and uploading all sizes of all images (3x3) but image shows on all sizes of all images selected is the image of Image C.
Can you please help on this issue that where I am wrong?
Thanks.
form.php
<form action="process.php" method="post" enctype="multipart/form-data">
<div class="col-lg-12 col-md-9 col-sm-12" id="thumb-output">
<div class="m-dropzone dropzone m-dropzone--primary" id="m-dropzone-two">
<h3 class="m-dropzone__msg-title">
Drop files here or click to upload.
</h3>
<input id="files" class="" type="file" name="file[]" multiple>
</div>
</div>
</form>
process.php
$change="";
$abc="";
if(count($_FILES['file']['name']) > 0){
$Kv = 0;
define ("MAX_SIZE","12000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$image =$_FILES["file"]["name"][$Kv];
if ($image) {
foreach($_FILES['file']['name'] as $filename) {
$filename = stripslashes($_FILES['file']['name'][$Kv]);
$extension = getExtension($filename);
$extension = strtolower($extension);
}
foreach($_FILES['file']['size'] as $size) {
$size=filesize($_FILES['file']['tmp_name'][$Kv]);
}
if ($size > MAX_SIZE*1024){
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'][$Kv];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$Kv];
$src = imagecreatefrompng($uploadedfile);
}else {
$src = imagecreatefromgif($uploadedfile);
}
//echo $scr;
list($width,$height)=getimagesize($uploadedfile);
////// 1st Size of Image
$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
////// 2nd Size of Image
$newwidths=400;
$newheights=400;
$tmps=imagecreatetruecolor($newwidths,$newheights);
imagecopyresampled($tmps,$src,0,0,0,0,$newwidths,$newheights,$width,$height);
////// 3rd Size of Image
$newwidthsz=92;
$newheightsz=92;
$tmpsz=imagecreatetruecolor($newwidthsz,$newheightsz);
imagecopyresampled($tmpsz,$src,0,0,0,0,$newwidthsz,$newheightsz,$width,$height);
/////////////////////////////////////////////////////
foreach($_FILES['file']['name'] as $name) {
$name = $_FILES["file"]["name"][$Kv];
$ext = end((explode(".", $name))); # extra () to prevent notice
}
$folderPath = "../images/combo2_images";
if (file_exists($folderPath)){
}else{
mkdir("$folderPath");
}
//mkdir($folderPath);
if ($execute == true) {
if ($type == 'm') {
foreach($_FILES['file']['name'] as $filenames) {
$filenames = "../images/combo2_mimages/m".$idz.'_'.$Kv++.'.'.$ext;
imagejpeg($tmps,$filenames,100);
}
//$savefilenames = 'sc1_'.$id.'.'.$ext;
foreach($_FILES['file']['name'] as $filenamesz) {
$filenamesz = "../images/combo2_mimages/small/m".$idz.'_'.$Kv++.'.'.$ext;
imagejpeg($tmpsz,$filenamesz,100);
}
//$savefilenamesz = 'sc1_'.$id.'.'.$ext;
}
$filename = "../images/combo2_images/".$idz.'.'.$ext;
imagejpeg($tmp,$filename,100);
$savefilename = $idz.'.'.$ext;
if ($_FILES['file']['size'] !== 0 && $_FILES['file']['error'] == 0) {
if ($type == 'm') {
$querys = "insert into items_images
(combo_type, combo_id, item_id, filename, status)
values (2, '$idz', '$mitem', '$savefilename', '$status')
";
$executes = $dba->query($querys);
}
$queryu = "update items_combobox2
set filename = '$savefilename'
where id = '$idz'
";
$executeu = $dba->query($queryu);
imagedestroy($src);
imagedestroy($tmp); ////// 1st Size of Image
imagedestroy($tmps); ////// 2nd Size of Image
imagedestroy($tmpsz); ////// 3rd Size of Image
}
}
}
header("location: all/items/");
echo "<script>parent.document.location.href = 'all/items/';</script>";
exit();
}
}
Finally i got i working as i want.
if(count($_FILES['file']['name']) > 0){
define ("MAX_SIZE","12000");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
$i=1; // auto increment number
$uploaddir = "../images/combo2_images"; //a directory inside
foreach ($_FILES['file']['name'] as $name => $value) {
$filename = stripslashes($_FILES['file']['name'][$name]);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
echo "\n This is the extension: ",$extension;
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
//print error message
?>
<h4>Unknown extension!</h4>
<?php
$errors=1;
} else {
$size=filesize($_FILES['file']['tmp_name'][$name]);
if ($size > MAX_SIZE*1024) {
?>
<h4>You have exceeded the size limit!</h4>
<?php
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" && $extension!=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="jpg" || $extension=="jpeg" && $extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension!=="jpg" || $extension!=="jpeg" && $extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'][$name];
$src = imagecreatefrompng($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=350;
$newheight=350;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$newwidths=400;
$newheights=400;
$tmps=imagecreatetruecolor($newwidths,$newheights);
imagecopyresampled($tmps,$src,0,0,0,0,$newwidths,$newheights,$width,$height);
$newwidthsz=92;
$newheightsz=92;
$tmpsz=imagecreatetruecolor($newwidthsz,$newheightsz);
imagecopyresampled($tmpsz,$src,0,0,0,0,$newwidthsz,$newheightsz,$width,$height);
//$image_name=($i++).$filename.'.'.$extension;
//$newname="files/".$image_name;
//$copied = copy($_FILES['file']['tmp_name'][$name], $newname);
if ($execute == true) {
if ($type == 'm') {
$filenames = "../images/combo2_mimages/m".$idz.'.'.$extension;
//imagejpeg($tmps,$filenames,100);
$copied1 = copy(imagejpeg($tmps,$filenames,100));
$filenamesz = "../images/combo2_mimages/small/m".$idz.'_'.($i++).'.'.$extension;
//imagejpeg($tmpsz,$filenamesz,100);
$copied2 = copy(imagejpeg($tmpsz,$filenamesz,100));
}
$filename = "../images/combo2_images/".$idz.'.'.$extension;
imagejpeg($tmp,$filename,100);
$savefilename=$idz.'.'.$extension;
if ($type == 'm') {
$querys="insert into items_images
(combo_type, combo_id, item_id, filename, status)
values (2, '$idz', '$mitem', '$savefilename', '$status')
";
$executes=$dba->query($querys);
}
$queryu="update items_combobox2
set filename = '$savefilename'
where id = '$idz'
";
$executeu=$dba->query($queryu);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmps);
imagedestroy($tmpsz);
}
if (!$copied) {
?>
<h4>Copy unsuccessfull!</h4>
<?php
$errors=1;
}
}
}
header("location: all/items/");
echo "<script>parent.document.location.href = 'all/items/';</script>";
exit();
}
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
So I've been banging my head against my desk for about a couple of days now trying to figure out is causing this 500 Error. every time I try and post a file image its throws me a error : POST http://example.com/server/uploadProfile.php 500 (Internal Server Error)
Not to sure exactly what it means and how to go about and fixing it.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
function image_upload($max_size,$input_file,$path,$new_width) {
if(!define("MAX_SIZE",$max_size)) {
define("MAX_SIZE",$max_size);
}
$errors = 0;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$img = $_FILES[$input_file]['name'];
$tmp_file = $_FILES[$input_file]['tmp_name'];
}
if($img) {
$file_name = stripcslashes($_FILES[$input_file]['name']);
$extension = getExtension($file_name);
$extension = strtolower($extension);
if($extension != 'jpg' && $extension != 'jpeg' && $extension != 'png' && $extension != 'gif') {
//echo "Unknown image type.";
$errors = 1;
return "Wrong file type.";
}
else {
$size = filesize($_FILES[$input_file]['tmp_name']);
if($size > MAX_SIZE*1024) {
$error = 1;
return "File size too big";
//echo "You have exceeded the size limit";
}
if($extension == 'jpg' || $extension == 'jpeg') {
$uploadedfile = $_FILES[$input_file]['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension == 'png') {
$uploadedfile = $_FILES[$input_file]['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else if($extension == 'gif') {
$uploadedfile = $_FILES[$input_file]['tmp_name'];
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newheight = ($height/$width)*$new_width;
$tmp = imagecreatetruecolor($new_width,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$new_width,$newheight,$width,$height);
$rnd = rand(0,9999999999999999999999);
$file_name = $rnd.'_'.$_FILES[$input_file]['name'];
$upload_path = $path .$rnd.'_'.$_FILES[$input_file]['name']
switch($_FILES[$input_file]['type']) {
case 'image/jpeg':
imagejpeg($tmp, $upload_path, 100);
break;
case 'image/jpg' :
imagejpeg($tmp, $upload_path, 100);
break;
case 'image/png':
imagepng($tmp, $upload_path, 0);
break;
case 'image/gif' :
imagegif($tmp, $upload_path);
break;
}
}
}
}
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
if(isset($_FILES['profilePhoto']['tmp_name'])) {
image_upload("320000","profilePhoto","/profile",250);
}
?>
$upload_path = $path .$rnd.'_'.$_FILES[$input_file]['name']
You are missing a ; at the end of the line. Syntax errors that make the script uncompilable will result in a server error 500
I have a file uploading code. everything is ok but i want to rename the file when it up loaded. my code is here. i need only the rename function and where it should be put. this code is very long because there crop the big image to small.
example: if file is 10MB it will be 40 or 50 KB
error_reporting(0);
$change = "";
$abc = "";
define("MAX_SIZE", "400");
function getExtension($str) {
$i = strrpos($str, ".");
if (!$i) {
return "";
} $l = strlen($str) - $i;
$ext = substr($str, $i + 1, $l);
return $ext;
}
$errors = 0;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$image = $_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$change = '<div class="msgdiv">Unknown Image extension</div>';
$errors = 1;
} else {
$size = filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE * 1024) {
$change = '<div class="msgdiv">You have exceeded the size limit!</div>';
$errors = 1;
} if ($extension == "jpg" || $extension == "jpeg") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
} else if ($extension == "png") {
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
} echo $scr;
list($width, $height) = getimagesize($uploadedfile);
$newwidth = 280;
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$filename = "../adimages/" . $_FILES['file']['name'];
imagejpeg($tmp, $filename, 100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
Just use the name you want instead of $_FILES['file']['name']
For example:
$filename = "../adimages/". time();
This will name each uploaded file with the current timestamp.
change Your below line
$filename = "../adimages/" . $_FILES['file']['name'];
with the below:-
$new_name = "new_file_name".time();
$filename = "../adimages/" . $new_name;
time() is added for uniqueness. and at the place of new_file_name , You can also put $_FILES['file']['name'];
try this --
just replace your line
$filename = "../adimages/" . $_FILES['file']['name'];
with this new line
$filename = "../adimages/" .time().'.'.$extension ;
I have a problem with image uploading in php
I want uploading with resizing image to width=600px
ex, when I uploaded an image with width of 2000px it should be uploaded with width of 600px
and of course smaller size on disk...
php file is:
<?php
require_once("db.php");
$name = trim($_POST['name']);
$addr = trim($_POST['addr']);
$dist = trim($_POST['dist']);
$city = trim($_POST['city']);
$phone = trim($_POST['phone']);
$price = trim($_POST['price']);
$lati = trim($_POST['lati']);
$long = trim($_POST['long']);
$tid = trim($_POST['type']);
$img = "";
if($_FILES)
{
//var_dump($_FILES);
$random_str = md5(uniqid(mt_rand(), true));
$f_name = "tmp/".$random_str.".jpg";
move_uploaded_file($_FILES['placeimg']['tmp_name'], $f_name);
$img = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$f_name;
$uploadedfile = $_FILES['placeimg']['tmp_name'];
$size=filesize($_FILES['placeimg']['tmp_name']);
$uploadedfile =$_FILES['placeimg']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($_FILES['placeimg']['tmp_name']);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
//$filename = "tmp/". $_FILES['file']['name'];
imagejpeg($tmp,$uploadedfile,75);
imagedestroy($src);
}
$sql = "INSERT INTO `Place`(`PName`, `PAddr`, `PDistrict`, `PCity`, `PImage`, `PPhone`, `PPrice`, `PLat`, `PLong`, `TID`, `PStatus`) VALUES ('{$name}', '{$addr}', '{$dist}', '{$city}', '{$img}', '{$phone}', '{$price}', '{$lati}', '{$long}', '{$tid}', '0')";
$status = 0;
$mess = "Err!";
if(mysql_query($sql))
{
$status = 1;
$mess = "Successful! Waiting approve";
}
$json['status'] = $status;
$json['message'] = $mess;
echo json_encode($json);
?>
the result is a big image without resizing
can you help?
try this:
function upload_resize($file,$newwidth,$resolution,$location,$prefix){
define ("MAX_SIZE","2048");
error_reporting(0);
$image =$file["name"];
$uploadedfile = $file['tmp_name'];
if ($image) {
$filename = stripslashes($file['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
//Unknown Image extension
return 1;
}
else
{
$size=filesize($file['tmp_name']);
if ($size > MAX_SIZE*1024){
//You have exceeded the size limit
return 2;
}
if($extension=="jpg" || $extension=="jpeg" ) {
$uploadedfile = $file['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png") {
$uploadedfile = $file['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else {
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $location.$prefix.$file['name'];
imagejpeg($tmp,$filename,$resolution);
imagedestroy($src);
imagedestroy($tmp);
}
}
return 0;
}
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// get file from user and send to this function:
$res=upload_resize($_FILES["image"],600,100,'../images','');
switch ($res){
case 0 : echo 'images saved'; break;
case 1 : echo 'Unknown file type'; break;
case 2 : echo 'file size error'; break;
}
i use this function for upload and resize image.
// for outputting a jpeg image, Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($tmp,$uploadedfile,75);
try this..
You create a new pattern file (using imagecreatetruecolor), but u didn't use a original picture.
Try use imagecopyresampled
like this:
<?php
require_once("db.php");
$name = trim($_POST['name']);
$addr = trim($_POST['addr']);
$dist = trim($_POST['dist']);
$city = trim($_POST['city']);
$phone = trim($_POST['phone']);
$price = trim($_POST['price']);
$lati = trim($_POST['lati']);
$long = trim($_POST['long']);
$tid = trim($_POST['type']);
$img = "";
if($_FILES)
{
//var_dump($_FILES);
$random_str = md5(uniqid(mt_rand(), true));
$f_name = "tmp/".$random_str.".jpg";
move_uploaded_file($_FILES['placeimg']['tmp_name'], $f_name);
$img = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$f_name;
$uploadedfile = $_FILES['placeimg']['tmp_name'];
$size=filesize($_FILES['placeimg']['tmp_name']);
$uploadedfile =$_FILES['placeimg']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($_FILES['placeimg']['tmp_name']);
$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$buff = imagecreatefromjpeg($uploadedfile);
imagecopyresampled($tmp, $b, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//$filename = "tmp/". $_FILES['file']['name'];
imagejpeg($tmp,$uploadedfile,75);
imagedestroy($src);
}
$sql = "INSERT INTO `Place`(`PName`, `PAddr`, `PDistrict`, `PCity`, `PImage`, `PPhone`, `PPrice`, `PLat`, `PLong`, `TID`, `PStatus`) VALUES ('{$name}', '{$addr}', '{$dist}', '{$city}', '{$img}', '{$phone}', '{$price}', '{$lati}', '{$long}', '{$tid}', '0')";
$status = 0;
$mess = "Err!";
if(mysql_query($sql))
{
$status = 1;
$mess = "Successful! Waiting approve";
}
$json['status'] = $status;
$json['message'] = $mess;
echo json_encode($json);
?>
If possible please i'd ask a favor, i've been trying to change this it but i haven't succeeded, i wanted to change it in my php file, but no result!
now this script below is a resize script, but it renames my file... i don't need it to do so, can you please see it and tell me how to remove the renaming of the file?
I know it has something to do with : $image->createFile(md5($tempFile)); i tried removing the md5 and still nothing...
Thanks!
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);
}
$this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
}
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'];
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'];
move_uploaded_file ($tempFile, $targetFile);
$image = new Image();
$image->setFile($targetFile);
$image->setUploadDir($targetPath);
$image->resize(800);
$image->createFile(md5($tempFile));
try changing to
$image->createFile('resized_'.$_FILES['Filedata']['name']);
function createFile($output_filename = null) {
if($this->ext == "JPG" OR $this->ext == "JPEG") {
imageJPEG($this->dst_r, $this->uploaddir.$output_filename, $this->quality);
} elseif($this->ext == "PNG") {
imagePNG($this->dst_r, $this->uploaddir.$output_filename);
} elseif($this->ext == "GIF") {
imageGIF($this->dst_r, $this->uploaddir.$output_filename);
}
$this->output = $this->uploaddir.$output_filename;
}
You confuse the file's actual name ($_FILES['Filedata']['name']) with the temporary name ($_FILES['Filedata']['tmp_name']). The first is the file the client uploaded. The second is a temporary file that the server created one the server's machine. So you have to use the actual, client-side name here.
This will make your day
$image->createFile(preg_replace("/\.[^\.]*$/", "", $targetFile));