////In your HTML View you can use this
<file name="upload[]" accept="image/*" multiple>
multiple and accept attribute is only available in HTML5 dosen't work in old browsers.
Шта ће Вам фејсбук и инстаграм ?
-направите свој Facebook & Instagram !
*Only one image:
<file name="upload[]" accept="image/*" multiple>
multiple and accept attribute is only available in HTML5 dosen't work in old browsers.
Шта ће Вам фејсбук и инстаграм ?
-направите свој Facebook & Instagram !
<?php
$files = null;
$errors = null;
define('IMAGE_TARGET',SITE_ROOT.'j2reimage'.DS.'image');
if(!empty($_POST['submitImage']))
{
$files = $_FILES['upload'];
$errors = array();
if(!empty($files))
{
//Move extension list to here you don't need redeclarate variables inside loop
$valid_extensions = array('jpeg','jpg','png','gif');
foreach($files as $file)
{
//Don't use explode for extensions
//$file_ext = explode('.',$file['name'][$key]);
//Use pathinfo
$path_parts = pathinfo($file);
$file_ext = $path_parts['extension'];
//$file_ext=strtolower(end(explode('.',$file['name'][$key])));
if(in_array($file_ext,$valid_extensions) === false)
{
$errors[] = ".$file_ext extension not allowed";
}
else
{
$filetmp = $file['tmp_name'][$key];
$dat = strftime("%Y-%m-%d %H:%M:%S", time());
if(file_exists(IMAGE_TARGET.DS.$file['name']))
{
//move_uploaded_file( $filetmp, $terget_path.'/'.$file['name'][$key]);
mysql_query("INSERT INTO 4j2memberimage VALUES ('', $memmem->id,'{$file['name']}', '$dat')");
//if(move_uploaded_file($filetmp, $terget_path.'/'.$file['name'][$key] ))
//Use copy insteds of move_uploaded_file sometimes you have right problems in some servers.
if(copy($filetmp, IMAGE_TARGET.DS.$file['name'] ))
{
$exe = explode(".", $file['name']);
$ext = $exe[1];
$w = 250;
$h = 250;
$target = IMAGE_TARGET.DS.$file['name'];
$newcopy = IMAGE_TARGET.DS.'thumb_'.DS.$file['name'];
list($w_orig, $h_orig) = getimagesize($target);
$scale_ratio = $w_orig / $h_orig;
if (($w / $h) > $scale_ratio)
{
$w = $h * $scale_ratio;
}
else
{
$h = $w / $scale_ratio;
}
$img = '';
$ext = strtolower($ext);
switch($ext) {
case 'gif':
$img = imagecreatefromgif($target);
break;
case 'png':
$img = imagecreatefrompng($target);
break;
case 'jpeg':
case 'jpg':
$img = imagecreatefromjpeg($target);
break;
}
$tci = imagecreatetruecolor($w, $h);
imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
if(@imagejpeg($tci, $newcopy, 80))
{
//return true;
$target_path = $target;
//$terger_path = SITE_ROOT.DS.$this->img_path.DS.$this->filename;
file_put_contents($target_path,$img);
//return unlink($target_path) ? true : false;
}
}
//end of if file_exists
}
else
{
$errors[] = 'Upload extention could not be located';
}
}
//redirect_to('inb.php');
}//foreach
}
else
{
$errors[] = ' The file could not be empty ';
}//if
}//if
*Only one image:
<?php
$name
=
''
;
$type
=
''
;
$size
=
''
;
$error
=
''
;
function
compress_image(
$source_file
,
$destination_file
,
$quality
) {
$info
=
getimagesize
(
$source_file
);
if
(!
empty
(
$info
)) {
switch
(
$info
[
'mime'
]) {
case
'image/jpeg'
:
// set quality
if
(
$quality
==
''
||
$quality
< 0 ||
$quality
> 100)
$quality
= 80;
// create a new image from the file
$image
= imagecreatefromjpeg(
$source_file
);
imagejpeg(
$image
,
$destination_file
,
$quality
);
return
$destination_file
;
break
;
case
'image/png'
:
// set quality
if
(
$quality
==
''
||
$quality
< 0 ||
$quality
> 9)
$quality
= 6;
// create a new image from the file
$image
= imagecreatefrompng(
$source_file
);
imagepng(
$image
,
$destination_file
,
$quality
);
return
$destination_file
;
break
;
case
'image/gif'
:
// set quality
if
(
$quality
==
''
||
$quality
< 0 ||
$quality
> 100)
$quality
= 70;
// create a new image from the file
$image
= imagecreatefromgif(
$source_file
);
imagegif(
$image
,
$destination_file
,
$quality
);
return
$destination_file
;
break
;
}
}
}
if
(
$_POST
) {
if
(
$_FILES
[
"file"
][
"error"
] > 0) {
$error
=
$_FILES
[
"file"
][
"error"
];
}
else
if
((
$_FILES
[
"file"
][
"type"
] ==
"image/gif"
) ||
(
$_FILES
[
"file"
][
"type"
] ==
"image/jpeg"
) ||
(
$_FILES
[
"file"
][
"type"
] ==
"image/png"
)) {
switch
(
$_FILES
[
"file"
][
"type"
]) {
case
'image/jpeg'
:
$url
=
'destination.jpg'
;
break
;
case
'image/png'
:
$url
=
'destination.png'
;
break
;
case
'image/gif'
:
$url
=
'destination.gif'
;
break
;
}
$filename
= compress_image(
$_FILES
[
"file"
][
"tmp_name"
],
$url
, 80);
$buffer
=
file_get_contents
(
$url
);
/* Force download dialog... */
header(
"Content-Type: application/force-download"
);
header(
"Content-Type: application/octet-stream"
);
header(
"Content-Type: application/download"
);
/* Don't allow caching... */
header(
"Cache-Control: must-revalidate, post-check=0, pre-check=0"
);
/* Set data type, size and filename */
header(
"Content-Type: application/octet-stream"
);
header(
"Content-Transfer-Encoding: binary"
);
header(
"Content-Length: "
.
strlen
(
$buffer
));
header(
"Content-Disposition: attachment; filename=$filename"
);
/* Send our file... */
echo
$buffer
;
}
else
{
$error
=
"Uploaded image should be jpg or gif or png"
;
}
}
?>
<html>
<head>
<title>Php code to compress the image file size</title>
</head>
<body>
<div
class
=
"message"
>
<?php
if
(
$_POST
){
if
(
$error
) {
?>
<label
class
=
"error"
><?php
echo
$error
; ?></label>
<?php
}
}
?>
</div>
<fieldset>
<legend>Upload Image:</legend>
<form action=
""
name=
"upload-form"
id=
"upload-form"
method=
"post"
enctype=
"multipart/form-data"
>
<ul>
<li>
<label>Upload:</label>
<input type=
"file"
name=
"file"
id=
"file"
/>
</li>
<li>
<input type=
"submit"
name=
"submit"
id=
"submit"
class
=
"submit"
/>
</li>
</ul>
</form>
</fieldset>
</body>
</html>
No comments:
Post a Comment
Коментар: