Просто ти го копирах и пейстнах + малки промени от едно нещо, което правя сега.$pic е в моя случай адрес на картинката, но в твоя случай трябва да е $_FILES['name-pole-za-kacvhane']['tmp_name']
$picinfo = getimagesize($pic);
$type = end(explode('/', $picinfo['mime']));
if ($type == 'png') {
$newimg = imagecreatefrompng($pic);
} elseif ($type == 'gif') {
$newimg = imagecreatefromgif($pic);
} else {
$newimg = imagecreatefromjpeg($pic);
}
if ($type == 'png') {
imagepng($newimg, $path . '/pics/s/' . $name);
} elseif ($type == 'gif') {
imagegif($newimg, $path . '/pics/s/' . $name);
} else {
imagejpeg($newimg, $path . '/pics/s/' . $name);
}
imagedestroy($newimg);
Тук една функцийка съм си направил в случай, че се наложи да ползвам това смаляване повече от веднъж.
function cthumb($img, $name, $sx=90, $sy=90) {
if ($GLOBALS['type'] == 'png') {
$image = imagecreatefrompng($img);
} elseif ($GLOBALS['type'] == 'gif') {
$image = imagecreatefromgif($img);
} else {
$image = imagecreatefromjpeg($img);
}
$iw = imagesx($image);
$ih = imagesy($image);
$nw = $sx;
$nh = $sy;
$newimg = imagecreatetruecolor($nw, $nh);
imagecopyresized($newimg, $image, 0, 0, 0, 0, $nw, $nh, $iw, $ih);
if ($GLOBALS['type'] == 'png') {
imagepng($newimg, 'mqsto + ime primer: pics/moqta_kartinka.jpeg');
} elseif ($GLOBALS['type'] == 'gif') {
imagegif($newimg, 'mqsto + ime primer: pics/moqta_kartinka.jpeg');
} else {
imagejpeg($newimg, 'mqsto + ime primer: pics/moqta_kartinka.jpeg');
}
imagedestroy($newimg);
}
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
//php
<?php
function cthumb($img, $name, $sx=90) {
if ($GLOBALS['type'] == 'png') {
$image = imagecreatefrompng($img);
} elseif ($GLOBALS['type'] == 'gif') {
$image = imagecreatefromgif($img);
} else {
$image = imagecreatefromjpeg($img);
}
$iw = imagesx($image);
$ih = imagesy($image);
$nw = $sx;
$nh = $ih*($nw/$iw);
$newimg = imagecreatetruecolor($nw, $nh);
imagecopyresized($newimg, $image, 0, 0, 0, 0, $nw, $nh, $iw, $ih);
if ($GLOBALS['type'] == 'png') {
imagepng($newimg, 'mqsto + ime primer: pics/moqta_kartinka.jpeg');
} elseif ($GLOBALS['type'] == 'gif') {
imagegif($newimg, 'mqsto + ime primer: pics/moqta_kartinka.jpeg');
} else {
imagejpeg($newimg, 'mqsto + ime primer: pics/moqta_kartinka.jpeg');
}
imagedestroy($newimg);
}
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
cthumb("upload/" . $_FILES["file"]["name"],"thumb1_".$_FILES["file"]["name"]);
cthumb("upload/" . $_FILES["file"]["name"],"thumb2_".$_FILES["file"]["name"],150,150);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Би трябвало да работи
if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
cthumb($upload_dir.$file_name,"thumb1_".$_FILES["file"]["name"]);
cthumb($upload_dir.$file_name,"thumb2_".$_FILES["file"]["name"],150,150);
echo "<center>Файла е качен успешно! <br><br>";
exit();
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="snimka" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
function thumbc($pic) {
$new_name = 'thumb' . end(explode('/', $pic));
if (end(explode('.', $new_name)) == 'jpeg') {
$img = imagecreatefromjpeg($pic);
} elseif (end(explode('.', $new_name)) == 'jpg') {
$img = imagecreatefromjpeg($pic);
} elseif (end(explode('.', $new_name)) == 'png') {
$img = imagecreatefrompng($pic);
} elseif (end(explode('.', $new_name)) == 'gif') {
$img = imagecreatefromgif($pic);
}
$ix = imagesx($img);
$iy = imagesy($img);
$tx = 222;
$ty = $iy * ($tx / $ix);
$new_img = imagecreatetruecolor($tx, $ty);
imagecopyresized($new_img, $img, 0, 0, 0, 0, $tx, $ty, $ix, $iy);
if (end(explode('.', $new_name)) == 'jpeg') {
imagejpeg($new_img, $GLOBALS['path'] . 'upload/' . $new_name);
} elseif (end(explode('.', $new_name)) == 'jpg') {
imagejpeg($new_img, $GLOBALS['path'] . 'upload/' . $new_name);
} elseif (end(explode('.', $new_name)) == 'png') {
imagepng($new_img, $GLOBALS['path'] . 'upload/' . $new_name);
} elseif (end(explode('.', $new_name)) == 'gif') {
imagegif($new_img, $GLOBALS['path'] . 'upload/' . $new_name);
}
imagedestroy($new_img);
imagedestroy($img);
}
$path = dirname(__FILE__) . DIRECTORY_SEPARATOR
if ($_FILES['snimka']['size'] >= 10048576) {
$err[] = 'Снимката е прекалено голяма';
}
if ($_FILES['snimka']['type'] != 'image/jpeg' &&
$_FILES['snimka']['type'] != 'image/pjpeg' &&
$_FILES['snimka']['type'] != 'image/gif' &&
$_FILES['snimka']['type'] != 'image/png') {
$err[] = 'Невалиден формат на снимката';
}
if (count($err) == 0) {
$picname = time() . '-' . strtolower($_FILES['snimka']['name']);
move_uploaded_file($_FILES['snimka']['tmp_name'], $path . 'syspics/gallery/' . $picname);
thumbc($path . 'upload/' . $picname);
if (!is_file($path . 'upload/thumb' . $picname)) {
unlink($path . 'upload/' . $picname);
die('<h1>Възникна грешка. Моля опитайте отново</h1>');
}
}
<?php
require_once('includes/thumb/ThumbLib.inc.php');
$pics = getcwd(). '/uploads/' ; # Директория на снимките
$thumbs = getcwd() . '/uploads/thumb/'; # Директория на Thumbnali
function makeThumb($thumb = '') {
global $pics, $thumbs;
if (!is_dir($pics)) #Проверяваме дали случайно директориите липсват
mkdir($pics); #Е ако ги няма създаваме ги
if(!is_dir($thumbs))
mkdir($thumbs);
$thumbname = $thumbs . $thumb;
$thumb = PhpThumbFactory::create($thumb); # Зареждаме снимката
$thumb->resize(75, 95); # Width , Height режем
$thumb->save($thumbname); # Запаметяваме
}
if (isset($_POST['upload'])) {
$ext = strstr($_FILES['image']['name'],"."); # Взимаме разширението на файла
if ($ext != '.jpg' && $ext != '.JPG' && $ext != '.bmp' && $ext != '.BMP' && $ext != '.gif' && $ext != '.GIF' && $ext != '.gif' && $ext != '.png' && $ext != '.PNG')
die('Файла не е снимка!');
$target = $pics . $_FILES['image']['name'] . $ext; # Къде ще се запише снимката ..
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) { #Проверяваме дали снимката е била качена
chdir($pics); #Влизаме при снимките
$thumb = makeThumb($_FILES['image']['name'] . $ext); #Създаваме Thumbs
echo 'Файла е успешно качен със успешно създаден Thumbnali';
}
else
die('Грешка!');
}
echo'<form enctype="multipart/form-data" action="" method="POST">
<body>
<table>
<tbody>
<tr>
<td> Снимка </td>
<td> <input type="file" name="image"> </td>
</tr>
<tr>
<td> <input type="submit" name="upload" value="Качи!"> </td>
</tr>
</tbody>
</table>
</body>
</form>';
?>