2015-09-01 33 views
0

這是我的index.php文件,我已經上傳並將圖像從tmp文件夾移動到圖像文件夾,並且它正常工作。但現在我想修剪顯示圖像,每張圖片應該顯示相同的大小,我已經嘗試了很多方法,但它不工作。 需要幫助!如何用PHP修剪圖像?

<?php 
require_once('appvars.php'); 
include 'db_connect.php'; 

$sql = "SELECT * FROM gitarwar"; 
$data = mysqli_query($dbc, $sql); 

echo "<table>"; 
while ($row = mysqli_fetch_array($data)) 
{ 
    echo '<tr><td>'; 
    echo '<strong>' . $row['Score'] . '</strong><br/>'; 
    echo '<strong>Name:</strong>' . $row['Name'] . '<br/>'; 
    echo '<strong>Datetime:</strong>' . $row['Datetime'] . '<br/>'; 
    echo '<img src="' .GW_UPLOADPATH . $row['Screenshot'] . '" alt="Score image" />'; 
    echo "</tr>"; 
} 
echo"</table>"; 
mysqli_close($dbc); 
?> 
+1

這是一個CSS問題,不一定是PHP。只需將寬度和高度設置爲你喜歡的內聯或者在一個css文件中 – Crecket

+0

或者修剪圖片服務器端:https://stackoverflow.com/questions/1654449/with-php-gd-how-do-i-trim- an-image?rq = 1 –

+0

但實際上我想用php做而不是css –

回答

1

我之前有過類似的任務。我的工作是檢查圖像,如果它比我的$newWidth$newHeight小,它補充說。

$imageUrl = [PATH OR URL TO YOUR IMAGE FILE]; 

$imageContent = file_get_contents($imageUrl); 
$im = imagecreatefromstring($imageContent); 
$width = imagesx($im); 
$height = imagesy($im); 

$newwidth = 300; 
$newheight = 300; 

$output = imagecreatetruecolor($newwidth, $newheight); 

imagealphablending($output, false); 
$transparency = imagecolorallocatealpha($output, 0, 0, 0, 127); 
imagefill($output, 0, 0, $transparency); 
imagesavealpha($output, true); 

imagecopymerge($output, $im, ($width < 300 ? (300 - $width)/2 : 0), ($height < 300 ? (300 - $height)/2 : 0), 0, 0, $width, $height, 100); 

//Show the image 
header('Content-Type: image/png'); 
imagepng($output); //You can save it with another parameter what is a path 
imagedestroy($output); 
imagedestroy($im); 

您需要將此行更改:​​

imagecopymerge($output, $im, ($width < 300 ? (300 - $width)/2 : 0), ($height < 300 ? (300 - $height)/2 : 0), 0, 0, $width, $height, 100); 

到你的尺寸。

這其實並不完全是你想要的,但它可能是一個很好的起點。

0

您可以使用imagecreatefrom(jpeg|png|gif)函數在上傳時修剪圖像。

$file = "images/" . $_FILES['image']['name']; 
$tmp_file = $_FILES['image']['tmp_name']; 
$type = $_FILES['images']['type']; 
if (move_uploaded_file($tmp_file, $file)) { 
     chmod($file, 0777); 
     // check the type of image, you can do this for other types too, just change the imagecreatefrom function to the type you want to save the image as 
     if ($type === 'image/jpeg') { 
      $jpeg = imagecreatefromjpeg($file); 
      $jpeg_width = imagesx($jpeg); 
      $jpeg_height = imagesy($jpeg); 
      $new_width = imagesx($jpeg)/4;   // Fix the width of the thumb nail images, change the width of image here 
      $new_height = imagesy($jpeg)/4; // change the height of image here 

      //new image 
      $new_image = imagecreatetruecolor($new_width, $new_height); 
      imagecopyresampled($new_image, $jpeg, 0, 0, 0, 0, $new_width, $new_height, $jpeg_width, $jpeg_height); 
      imagejpeg($new_image, $thumb); 
      chmod($thumb, 0777); 
     } 
    }