简体   繁体   English

PHP中的图片上传概念

[英]Image uploading concept in PHP

how to take image from one folder and rename and re-size the images and move to another folder? 如何从一个文件夹中获取图像并重命名并重新设置图像大小并移动到另一个文件夹? I need to get image from one folder and rename and re-size the same image and move into another folder using php 我需要从一个文件夹中获取图像,并重命名并重新调整同一图像的大小,然后使用php移至另一个文件夹中

You will most likely be using gd for resizing the images. 您很可能会使用gd调整图像大小。 Here is a pretty crappy, but hopefully useful code sample. 这是一个很烂,但希望有用的代码示例。 In this case, $originalName is the name given in the $_FILES array's tmp_name position. 在这种情况下,$ originalName是$ _FILES数组的tmp_name位置中给出的名称。 I am resizing to a width of 1200 in this case, with the height adapting according to such width. 在这种情况下,我将尺寸调整为1200,高度根据该宽度进行调整。 You might (and most likely will) not desire this behavior. 您可能(并且很可能会)不希望这种行为。 This is just some ugly code I used in some courses I taught about 3 years ago, I don't have the newer samples in this computer so you will have to get used to the idea :) 这只是我三年前教的某些课程中使用的丑陋代码,我的计算机上没有较新的示例,因此您必须习惯这个主意:)

$newDir is where the file will be located. $ newDir是文件所在的位置。 by calling imagejpeg or imagepng and passing the filename as second argument, it means to the function that you wish to save the image in that location. 通过调用imagejpeg或imagepng并将文件名作为第二个参数传递,这意味着您希望将图像保存在该位置的函数。

if ($type == 'image/jpeg') {
    $original = imagecreatefromjpeg($originalName);
}
else {
    $original = imagecreatefrompng($originalName);
}
$width = imagesx($original);
$height = imagesy($original);
//prepare for creation of image with width of 1000
$new_height = floor($height * (1200 / $width));
// create the 1200 width image
$tmp_img = imagecreatetruecolor(1200, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $original, 0, 0, 0, 0, 
                 1200, $new_height, $width, $height);
//create a random and unique name to identify (here it isn't that random ;)

$newDir = '/this/is/some/directory/and/filename.';
if ($type == 'image/jpeg') {
    imagejpeg($tmp_img, $newDir."jpg");
}
else {
    imagepng($tmp_img, $newDir."png");
}

PHP已经内置了许多文件系统功能(例如, rename ),您将在此处找到使用GD库调整图像大小所需的大部分功能。

Use imagick library to resize; 使用imagick库调整大小; it's good. 很好。

If I were you I would write a PE using a diffent language (one that you might be best used to) to adjust anything of the given image then just feel free to phpexec it to do all the steps you mentioned, you can sit relax and wait for the end result. 如果我是我,我会使用一种不同的语言(可能是最适合的一种语言)来编写PE,以调整给定图像的任何内容,然后随意通过phpexec使其执行您提到的所有步骤,就可以放松并等待最终结果。 HHAHAHHA :-) 哈哈哈哈:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM