سؤال

أعمل على برنامج نصي يقوم بتحميل صورة باستخدام 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/source/browse/#svn/trunk/includes/classes

<?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/

تبدأ بتحميل الصورة وحفظها في ملف صورة مؤقتة. يعمل هذا البرنامج النصي على نموذج مع مدخلات لارتفاع أقصى أو أقصى عرض. لذلك سيقوم بعد ذلك بإنشاء ملف صورة جديد استنادًا إلى العرض/الارتفاع الجديد ثم نسخ صورة Temp إلى الصورة الجديدة التي تم إنشاؤها على الخادم.

ترى هذا بالرمز التالي:

// 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) سيؤدي هذا الرمز تلقائيًا إلى تغيير حجم الصور أثناء التحميل

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top