- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
基本上,我为我的网站使用 PHP 和 HTML。我是 PHP 的新手。因此,如果我在我的代码或方法中犯了任何错误,我请求您纠正我。
我已经编写了将用户上传的图像调整为特定大小(即特定宽度和高度)的代码。我想让上传的图片尺寸为940 px * 370 px。但是在这样做的同时我想处理以下问题:
为了实现上述功能,我编写了以下代码:
HTML 代码(upload_file.html):
<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 代码(upload_file.php):
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 5242880)
&& in_array($extension, $allowedExts)) {
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/upload" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//Store the name of the temporary copy of the file stored on the server
$images = $_FILES["file"]["tmp_name"];
/*Create a new file name for uploaded image file :
*prepend the string "upload" to it's original file name
*/
$new_images = "upload".$_FILES["file"]["name"];
//Copies a file contents from one file to another
//copy($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
$width = 940;
//Determine the size of a given image file and return the dimensions along with the file type
$size=GetimageSize($images);
//$height=round($width*$size[1]/$size[0]);
$height = 370;
//Create a new image from file or URL & returns an image identifier representing the image obtained from the given filename.
$images_orig = ImageCreateFromJPEG($images);
//Get image width of originally uploaded image
$photoX = ImagesX($images_orig);
//Get image height of originally uploaded image
$photoY = ImagesY($images_orig);
$scaleX = $width / $photoX;
$scaleY = $height / $photoY;
$scale = min($scaleX, $scaleY);
$scaleW = $scale * $photoX;
$scaleH = $scale * $photoY;
/*$width = $scale * $photoX;
$height = $scale * $photoY;*/
//Create a new true color image & returns an image identifier representing a black image of the specified size.
$images_fin = ImageCreateTrueColor($width, $height);
$background = ImageColorAllocate($images_fin, 0, 0, 0);
ImageFill($images_fin, 0, 0, $background);
/*Copy and resize part of an image with resampling
*copies a rectangular portion of one image to another image,
*smoothly interpolating pixel values so that, in particular,
*reducing the size of an image still retains a great deal of clarity.
*/
/*ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);*/
ImageCopyResampled($images_fin, $images_orig, $width / 2 - $scaleW / 2, $height / 2 - $scaleH / 2, 0, 0, $scaleW+1, $scaleH+1, $photoX, $photoY);
/*Output image to browser or file
*creates a JPEG file from the given image.
*/
ImageJPEG($images_fin,"upload/".$new_images);
/*Destroy an image
*frees any memory associated with image image.
*/
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
}
?>
注意:要测试上面的代码并查看上传的图像,请在文件 upload_file.html 和 upload_file.php 所在的同一目录中创建一个名为“上传”的文件夹,并具有适当的权限。
实际上,上面的代码对我有用,但它有以下几个问题:
.jpg
的图像文件发出警告。它不应该发生。您可以通过上传尺寸高于 940 像素 * 370 像素 且大小不超过 5 MB 的图像来检查本地计算机上的代码功能。 p>
如果有人能帮助我解决以上两个问题,那将对我有很大帮助。
最佳答案
It's giving warnings for image files with extensions other than .jpg. It should not happen.
您收到警告是因为您使用 JPEG 特定函数打开图像,无论格式如何:
// line 48
$images_orig = ImageCreateFromJPEG($images);
要解决此问题,您可以使用通用的 imagecreatefromstring
函数,它可以在不考虑图像格式的情况下打开图像。
// line 48
$images_orig = ImageCreateFromString(file_get_contents($images));
资源:
the image is saving to the server after modifications in it's dimensions (940 px * 370px). The quality of image saved to the server is same as of original image uploaded by user but it's adding additional black space in the background to the image. It should not happen.
这里有两个错误:
要实现它,您应该首先在目标图像中选择透明颜色:我习惯使用浅粉色 (#FF00FF) 作为透明,因为这不是图像上常见的颜色(如果您要上传花卉图片,选择另一种颜色 :-))。然后,在将源图像复制到目标图像之前,将背景颜色设置为浅粉色:整个图像将变得透明而不是黑色。
替换:
// line 67
$images_fin = ImageCreateTrueColor($width, $height);
$background = ImageColorAllocate($images_fin, 0, 0, 0);
ImageFill($images_fin, 0, 0, $background);
通过以下几行:
$images_fin = ImageCreateTrueColor($width, $height);
$transparent = ImageColorAllocate($images_fin, 255, 0, 255);
ImageFill($images_fin, 0, 0, $transparent);
ImageColorTransparent($images_fin, $transparent);
要解决这个问题,只需更换:
// line 31
$new_images = "upload" . $_FILES["file"]["name"];
// line 85
ImageJPEG($images_fin, "upload/" . $new_images);
// line 93
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
通过:
// line 31
$new_images = "upload" . $_FILES["file"]["name"] . '.png';
// line 85
ImagePNG($images_fin, "upload/" . $new_images);
// line 93
echo "Stored in: " . "upload/" . $new_images;
资源:
<?php
$allowedExts = array ("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 5242880) && in_array($extension,
$allowedExts))
{
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/upload" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
//Store the name of the temporary copy of the file stored on the server
$images = $_FILES["file"]["tmp_name"];
/* Create a new file name for uploaded image file :
* prepend the string "upload" to it's original file name
*/
$new_images = "upload" . $_FILES["file"]["name"] . '.png';
//Copies a file contents from one file to another
//copy($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
$width = 940;
//Determine the size of a given image file and return the dimensions along with the file type
$size = GetimageSize($images);
//$height=round($width*$size[1]/$size[0]);
$height = 370;
//Create a new image from file or URL & returns an image identifier representing the image obtained from the given filename.
$images_orig = ImageCreateFromString(file_get_contents($images));
//Get image width of originally uploaded image
$photoX = ImagesX($images_orig);
//Get image height of originally uploaded image
$photoY = ImagesY($images_orig);
$scaleX = $width / $photoX;
$scaleY = $height / $photoY;
$scale = min($scaleX, $scaleY);
$scaleW = $scale * $photoX;
$scaleH = $scale * $photoY;
/* $width = $scale * $photoX;
$height = $scale * $photoY; */
//Create a new true color image & returns an image identifier representing a black image of the specified size.
$images_fin = ImageCreateTrueColor($width, $height);
$transparent = imagecolorallocate($images_fin, 255, 0, 255);
imagefill($images_fin, 0, 0, $transparent);
imagecolortransparent($images_fin, $transparent);
/* Copy and resize part of an image with resampling
* copies a rectangular portion of one image to another image,
* smoothly interpolating pixel values so that, in particular,
* reducing the size of an image still retains a great deal of clarity.
*/
/* ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); */
ImageCopyResampled($images_fin, $images_orig, $width / 2 - $scaleW / 2, $height / 2 - $scaleH / 2, 0, 0,
$scaleW + 1, $scaleH + 1, $photoX, $photoY);
/* Output image to browser or file
* creates a JPEG file from the given image.
*/
ImagePNG($images_fin, "upload/" . $new_images);
/* Destroy an image
* frees any memory associated with image image.
*/
ImageDestroy($images_orig);
ImageDestroy($images_fin);
echo "Stored in: " . "upload/" . $new_images;
}
}
}
else
{
echo "Invalid file";
}
关于php - 如何使为调整图像大小而编写的代码适用于各种图像扩展并对其进行优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26311993/
我在我的 Xcode 项目目录中输入了以下内容: keytool -genkey -v -keystore release.keystore -alias mykey -keyalg RSA \
假设我有一个像这样的 DataFrame(或 Series): Value 0 0.5 1 0.8 2 -0.2 3 None 4 None 5 None
我正在对一个 Pandas 系列进行相对繁重的应用。有什么方法可以返回一些打印反馈,说明每次调用函数时在函数内部进行打印还有多远? 最佳答案 您可以使用跟踪器包装您的函数。以下两个示例,一个基于完成的
我有一个 DataFrame,其中一列包含列表作为单元格内容,如下所示: import pandas as pd df = pd.DataFrame({ 'col_lists': [[1, 2
我想使用 Pandas df.apply 但仅限于某些行 作为一个例子,我想做这样的事情,但我的实际问题有点复杂: import pandas as pd import math z = pd.Dat
我有以下 Pandas 数据框 id dist ds 0 0 0 0 5 1 0 0 7 2 0 0
这发生在我尝试使用 Gradle 构建时。由于字符串是对象,因此似乎没有理由发生此错误: No signature of method: java.util.HashMap.getOrDefault(
您好,有人可以解释为什么在 remaining() 函数中的 Backbone 示例应用程序 ( http://backbonejs.org/examples/todos/index.html ) 中
我有两个域类:用户 class User { String username String password String email Date dateCreated
问题陈述: 一个 pandas dataframe 列系列,same_group 需要根据两个现有列 row 和 col 的值从 bool 值创建。如果两个值在字典 memberships 中具有相似
apporable 报告以下错误: error: unknown type name 'MKMapItem'; did you mean 'MKMapView'? MKMapItem* destina
我有一个带有地址列的大型 DataFrame: data addr 0 0.617964 IN,Krishnagiri,635115 1 0.635428 IN,Chennai
我有一个列表list,里面有这样的项目 ElementA: Number=1, Version=1 ElementB: Number=1, Version=2 ElementC: Number=1,
我正在编译我的源代码,它只是在没有运行应用程序的情况下终止。这是我得到的日志: Build/android-armeabi-debug/com.app4u.portaldorugby/PortalDo
我正在尝试根据另一个单元格的值更改单元格值(颜色“红色”或“绿色”)。我运行以下命令: df.loc[0, 'Colour'] = df.loc[0, 'Count'].apply(lambda x:
我想弄清楚如何使用 StateT结合两个 State基于对我的 Scalaz state monad examples 的评论的状态转换器回答。 看来我已经很接近了,但是在尝试申请 sequence
如果我已经为它绑定(bind)了集合,我该如何添加 RibbonLibrary 默认的快速访问项容器。当我从 UI 添加快速访问工具项时,它会抛出 Operation is not valid whi
在我学习期间Typoclassopedia我遇到了这个证明,但我不确定我的证明是否正确。问题是: One might imagine a variant of the interchange law
我是一名优秀的程序员,十分优秀!