题
我工作的一个脚本,上传使用PHP和我的图片想使其调整图像在保存前宽180。结果 我尝试使用WideImage图书馆 - > saveFileTO(...),但是当我包括WideImage.php的网页,网页一片空白!点击 因此,这里是我的脚本,如果你能帮助我,告诉我如何让它保存调整后的版本,点击
解决方案
您可以使用 PHP GD库来调整上上传的图像。
下面的代码应该给你如何实现调整大小的一个想法:
// Get the image info from the photo
$image_info = getimagesize($photo);
$width = $new_width = $image_info[0];
$height = $new_height = $image_info[1];
$type = $image_info[2];
// Load the image
switch ($type)
{
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($photo);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($photo);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($photo);
break;
default:
die('Error loading '.$photo.' - File type '.$type.' not supported');
}
// Create a new, resized image
$new_width = 180;
$new_height = $height / ($width / $new_width);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save the new image over the top of the original photo
switch ($type)
{
case IMAGETYPE_JPEG:
imagejpeg($new_image, $photo, 100);
break;
case IMAGETYPE_GIF:
imagegif($new_image, $photo);
break;
case IMAGETYPE_PNG:
imagepng($new_image, $photo);
break;
default:
die('Error saving image: '.$photo);
}
其他提示
您可以使用我为这样一个任务,写一个类:
http://code.google。 COM / p / IMAGE2 /源/浏览/#SVN /中继/包括/类
<?php
try
{
$image = new Image2($path_to_image);
}
catch (NotAnImageException $e)
{
printf("FILE PROVIDED IS NOT AN IMAGE, FILE PATH: %s", $path_to_image);
}
$image -> resize(array("width" => 180)) -> saveToFile($new_path); // be sure to exclude the extension
$new_file_location = $image -> getFileLocation(); // this will include the extension for future use
您甚至不需要使用WideImage库。
在这里检查此脚本: http://bgallz.org/502/php-upload-resize-image/一>
您通过上传图片并保存到一个临时的图像文件开始。此脚本运行了与用于最大高度或最大宽度的输入的形式。因此,这将随后生成基于新的宽度/高度新的图像文件,然后将临时图像复制到服务器上创建新的。
您看到此用下面的代码:
// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight);
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
不使用任何库 检查这个脚本 http://dr-wordpress.blogspot.com/2013 /12/image-resizing-using-php.html 只是给了从imges质量(0-99) 此代码将自动调整图像,同时上载
不隶属于 StackOverflow