railsspace building a social networking website with ruby on rails phần 8

id = tùy chọn con số không. Đồng thời, chúng ta phải sửa đổi các tuyến đường Rails cho thư mục gốc của trang web của chúng tôi đưa vào tài khoản sự hiện diện của một id con số không:Bạn có thể có thư mục gốc của trang web của bạn được định tuyến bằng cách gắn | Simpo PDF Merge and Split Unregistered Version - http Manipulating avatars 377 eventual system call to the underlying convert executable will look something like this system convert . The appropriate value of convert with full path name will automatically be interpolated into the string used for the system call. The save method Now that we know how to convert images we come finally to the Avatar save method. save itself is somewhat of an anticlimax since we push the hard work into an auxiliary function called successful_conversion Listing app models class Avatar ActiveRecord Base Image sizes IMG_SIZE 240x300 THUMB_SIZE 50x64 Save the avatar images. def save successful_conversion end private Try to resize image file and convert to PNG. We use ImageMagick s convert command to ensure sensible image sizes. def successful_conversion Prepare the filenames for the conversion. source tmp @ _full_size full_size DIRECTORY filename thumbnail DIRECTORY thumbnail_name Ensure that small and large images both work by writing to a normal file. Small files show up as StringIO larger ones as Tempfiles. source wb f @ Convert the files. system convert source -resize IMG_SIZE full_size system convert source -resize THUMB_SIZE thumbnail source if source No error-checking yet return true end Simpo PDF Merge and Split Unregistered Version - http 378 Chapter 12 Avatars successful_conversion looks rather long but it s mostly simple. We first define file names for the image source full-size avatar and thumbnail and then we use the system command and our convert method to create the avatar images. We don t need to create the avatar files explicitly since convert does that for us. At the end of the function we return true indicating success thereby following the same convention as Active Record s save. This is bogus of course since the

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.