2015-08-12 3 views
0

Я загрузив изображение с формой, как это:Изменение размера изображения при загрузке PHP

include 'extraphp/config.php'; 

$title = $_POST['titleStory']; 
$excerpt = $_POST['excerptStory']; 
$story = $_POST['storyStory']; 
$catagory = $_POST['catagory']; 
$tags = $_POST['tagsStory']; 
$author = $_POST['authorStory']; 
$date = $_POST['dateStory']; 

/* Image Upload */ 

$image = $_FILES["imageStory"]; 
$image2= preg_replace("/[^A-Z0-9._-]/i", "_", $image["name"]); 

$target_dir = "../uploads/large/"; 
$target_file = $target_dir . $image2; 
move_uploaded_file($_FILES['imageStory']['tmp_name'], $target_file); 

$catagory2 = implode (", ", $catagory); 
$author2 = implode(',', $author); 

$sql="INSERT INTO story (titleStory,excerptStory,storyStory,catagory,tagsStory,authorStory,dateStory,imageStory) VALUES ('". $title ."','". $excerpt ."','". $story ."','". $catagory2 ."','". $tags ."','". $author2 ."','". $date ."','". $target_file ."')"; 

mysqli_query($con,$sql); 
header("location: dashboard.php") 

теперь я хочу, чтобы изменить размер изображения в 180px X 109px.
Любое тело помогает мне?

+0

Вы хотите использовать библиотеку изображений Г-сподь: http://php.net/manual/en/ref.image.php – Blake

+0

Когда вы сделали поиск Google для «PHP изменения размера образ ", вы нашли что-нибудь? – David

+0

попробуйте http://www.verot.net/php_class_upload.html или http://www.w3bees.com/2013/03/resize-image-while-upload-using-php.html У этих обоих есть код с пример. – Shridhar

ответ

2

Помните, что вам необходимо проверить расширение изображения, чтобы использовать код imagecreate... funtion. В примере используется всегда imagecreatefromjpeg(), предполагая, что все загруженные изображения - JPG.

list($imageWidth, $imageHeight) = getimagesize($target_file); 

$resampledImage = imagecreatetruecolor(180, 109); 

//Check file extension here to use the correct image create function 
//imagecreatefromjpeg(); imagecreatefrompng(); imagecreatefromgif() etc... 
$source = imagecreatefromjpeg($target_file); 

imagecopyresized($resampledImage, $source, 180, 109, $imageWidth, $imageHeight); 

ob_start(); 

//Check file extension here to use the correct image output function 
//imagejpeg(); imagegif(); imagepng() etc... 
imagejpeg($resampledImage, null, 100); 

$imageContent = ob_get_clean(); 

file_put_contents($target_file, $imageContent); 

http://php.net/manual/en/function.getimagesize.php

http://php.net/manual/en/function.imagecreatetruecolor.php

http://php.net/manual/en/function.imagecreatefromjpeg.php

http://php.net/manual/en/function.imagecreatefrompng.php

http://php.net/manual/en/function.imagecopyresized.php

http://php.net/manual/en/function.imagejpeg.php

http://php.net/manual/en/function.ob-start.php

http://php.net/manual/en/function.file-put-contents.php

+0

Можете ли вы поместить этот код в свой код, как marge оба кода? –

+0

Просто вставьте его под 'move_uploaded_file ($ _ FILES ['imageStory'] ['tmp_name'], $ target_file);' – Mario

+0

он покажет мне черную страницу размером 180x109 :( –

Смежные вопросы