>  기사  >  백엔드 개발  >  PHP에서 업로드된 파일과 이미지를 작동하는 방법

PHP에서 업로드된 파일과 이미지를 작동하는 방법

墨辰丷
墨辰丷원래의
2018-06-11 17:27:171350검색

이 글은 주로 PHP에서 업로드된 파일을 썸네일로 만드는 방법을 소개합니다. 업로드된 파일과 이미지 작업을 수행하는 PHP의 기술이 필요합니다. 도움이 필요한 친구들이 참고할 수 있습니다.

이 글의 예는 다음과 같습니다. 업로드된 파일에서 썸네일을 생성하는 방법의 PHP 구현 이야기입니다. 구체적인 구현 방법은 다음과 같습니다.

<?php
if ($_REQUEST[&#39;action&#39;]=="add"){
$userfile = $HTTP_POST_FILES[&#39;photo&#39;][&#39;tmp_name&#39;];
$userfile_name = $HTTP_POST_FILES[&#39;photo&#39;][&#39;name&#39;];
$userfile_size = $HTTP_POST_FILES[&#39;photo&#39;][&#39;size&#39;];
$userfile_type = $HTTP_POST_FILES[&#39;photo&#39;][&#39;type&#39;];
///////////////////////// 
//GET-DECLARE DIMENSIONS //
$dimension = getimagesize($userfile);
$large_width = $dimension[0]; // GET PHOTO WIDTH
$large_height = $dimension[1]; //GET PHOTO HEIGHT
$small_width = 120; // DECLARE THUMB WIDTH
$small_height = 90; // DECLARE THUMB HEIGHT
/////////////////////////
//CHECK SIZE //
if ($userfile_size>102400){
   $error=1;
   $msg = "The photo is over 100kb. Please try again.";
}
////////////////////////////////
// CHECK TYPE (IE AND OTHERS) //
if ($userfile_type="image/pjpeg"){
  if ($userfile_type!="image/jpeg"){
    $error=1;
    $msg = "The photo must be JPG";
  }
}
//////////////////////////////
//CHECK WIDTH/HEIGHT //
if ($large_width!=600 or$large_height!=400){
$error=1;
$msg = "The photo must be 600x400 pixels";
}
///////////////////////////////////////////
//CREATE THUMB / UPLOAD THUMB AND PHOTO ///
if ($error<>1){
  $image = $userfile_name; //if you want to insert it to the database
  $pic = imagecreatefromjpeg($userfile);
  $small = imagecreatetruecolor($small_width,$small_height);
  imagecopyresampled($small,$pic,0,0,0,0, $small_width, $small_height, $large_width, $large_height);
  if (imagejpeg($small,"path/to/folder/to/upload/thumb".$userfile_name, 100)){  
    $large = imagecreatetruecolor($large_width,$large_height);
  imagecopyresampled($large,$pic,0,0,0,0, $large_width, $large_height, $large_width, $large_height);
    if (imagejpeg($large,"path/to/folder/to/upload/photo".$userfile_name, 100))
   {}
      else {$msg="A problem has occured. Please try again."; $error=1;}
  }
  else {
   $msg="A problem has occured. Please try again."; $error=1;
  }
}
//////////////////////////////////////////////
/// If everything went right a photo (600x400) and
/// a thumb(120x90) were uploaded to the given folders
}
?>
<html><head><title>create thumb</title></head>
<body>
<form name="form1" enctype="multipart/form-data" action="thisfile.php?action=add" method="post">
Select Photo: <input type="file" name="photo">
<input type="submit" name="submit" value="CREATE THUMB AND UPLOAD">
</form>
</body
</html>

요약: 위 내용은 이 글의 전체 내용이므로, 모든 분들의 학습에 도움이 되기를 바랍니다.

관련 권장 사항:

php는 보도 자료 시스템을 구현합니다.

php는 이미지 크기 수정, 워터마킹, 인증 코드 생성, 출력 및 저장을 수행합니다.

php는 Sina Music Library 검색을 통해 노래를 검색합니다. 인터페이스 기능

위 내용은 PHP에서 업로드된 파일과 이미지를 작동하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.